本文介绍了在Android中的谷歌地图上绘制多个标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是Android领域的新手。我已经写了一个 android App1 ,它将从网络提供商获取经度和纬度值,并将其存储在我的本地服务器(LAMP)中。
I还创建了一个具有3列(lat,lon,id)的MYSQL数据库表,它具有使用网络提供程序检索到的值(lat和lon)。目前在我的表中有超过10个值。
我已经创建了JSON对象来从我的 Android App2中使用PHP脚本从MYSQL DB获取这些值。
所有这些事情都很好。我现在要做的是创建一个MapActivity,它将使用Marker在地图上绘制这些纬度和经度值。我试图在地图上绘制它,但我只在地图上获取了我的最后一个位置(来自我的表格的最后一个经纬度值)。
如何在地图上绘制我所有的点。以下是我尝试过的代码,但无法正常工作。请帮助我纠正我的错误。
public class ShowMapActivity扩展MapActivity {
String returnString;
字符串returnString1;
double aa1;
double aa2;
私人MapController mapController;
私人MapView地图查看;
私人LocationManager locationManager;
private MyOverlay itemizedoverlay;
私人MyLocationOverlay myLocationOverlay;
public void onCreate(Bundle bundle){
super.onCreate(bundle);
setContentView(R.layout.main); //将布局绑定到活动
//配置地图
mapView =(MapView)findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
//mapView.setSatellite(true);
mapController = mapView.getController();
mapController.setZoom(14); // Zoon 1是世界视图
locationManager =(LocationManager)getSystemService(Context.LOCATION_SERVICE);
myLocationOverlay = new MyLocationOverlay(this,mapView);
mapView.getOverlays()。add(myLocationOverlay);
myLocationOverlay.runOnFirstFix(new Runnable(){
public void run(){
mapView.getController()。animateTo(myLocationOverlay.getMyLocation());
}
});
可绘制drawable = this.getResources()。getDrawable(R.drawable.ic_launcher);
itemizedoverlay = new MyOverlay(this,drawable);
createMarker();
}
@Override
protected boolean isRouteDisplayed(){
return false;
}
private void createMarker(){
GeoPoint p = mapView.getMapCenter();
OverlayItem overlayitem = new OverlayItem(p,,);
itemizedoverlay.addOverlay(overlayitem);
if(itemizedoverlay.size()> 0){
mapView.getOverlays()。add(itemizedoverlay);
@Override
保护无效的onResume(){
super.onResume();
myLocationOverlay.enableMyLocation();
myLocationOverlay.enableCompass();
@Override
protected void onPause(){
super.onPause();
myLocationOverlay.disableMyLocation();
myLocationOverlay.disableCompass();
public class GetMethodEx {
public String getInternetData()throws Exception {
BufferedReader in = null;
String data =;
// String returnString = null;
// httpGet
try {
HttpClient client = new DefaultHttpClient();
URI website = new URI(http://192.168.1.15/latlonret1.php);
HttpGet request = new HttpGet();
request.setURI(网站);
HttpResponse response = client.execute(request);
in = new BufferedReader(new InputStreamReader(response
.getEntity()。getContent()));
StringBuffer sb = new StringBuffer();
String l =;
String nl = System.getProperty(line.separator); ((l = in.readLine())!= null){
sb.append(l + nl);
}
in.close();
data = sb.toString();
//返回数据;
} catch(Exception e){
Log.e(log_tag,Error conversion result+ e.toString());
//解析json数据
尝试{
JSONArray jArray = new JSONArray(data);
for(int i = 0; i< jArray.length(); i ++){
JSONObject json_data = jArray.getJSONObject(i);
JSONObject json_data1 = jArray.getJSONObject(i);
returnString = json_data.getString(lat)+\\\
;
returnString1 = json_data1.getString(lon)+\\\
;
System.out.println(returnString);
System.out.println(returnString1);
}
Bundle bundle = new Bundle();
bundle.putString(stuff,returnString);
bundle.putString(stuff1,returnString1);
尝试
{
aa1 = Double.valueOf(returnString);
aa2 = Double.valueOf(returnString1);
}
catch(Exception e)
{
}
System.out.println(returnString);
System.out.println(returnString1);
$ b catch(JSONException e){
Log.e(log_tag,Error parsing data+ e.toString() );
}
return returnString;
$ b 解决方案
a>可能有帮助。也有很多例子。
I am a newbie in android field. I have written a android App1 that will retrieve latitude and longitude values from Network Provider and stores it in my local server(LAMP).
I have also created a MYSQL DB table that has 3 columns(lat,lon,id) that has the values (lat and lon) which are retrieved using the Network Provider. Currently there are more than 10 values in my table.
I have created JSON object for getting those values from MYSQL DB using PHP script in my Android App2
All these things works fine. What I have to do now is to create a MapActivity which will plot those lat and lon values on map using Marker. I tried to plot it on map but I am getting only my last location(last lat and lon value from my table) on map.
How to plot all my points on map. Following is my code which I tried but it is not working properly. Please help me to rectify my mistake.
public class ShowMapActivity extends MapActivity {
String returnString;
String returnString1;
double aa1 ;
double aa2;
private MapController mapController;
private MapView mapView;
private LocationManager locationManager;
private MyOverlay itemizedoverlay;
private MyLocationOverlay myLocationOverlay;
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.main); // bind the layout to the activity
// Configure the Map
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
//mapView.setSatellite(true);
mapController = mapView.getController();
mapController.setZoom(14); // Zoon 1 is world view
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
myLocationOverlay = new MyLocationOverlay(this, mapView);
mapView.getOverlays().add(myLocationOverlay);
myLocationOverlay.runOnFirstFix(new Runnable() {
public void run() {
mapView.getController().animateTo(myLocationOverlay.getMyLocation());
}
});
Drawable drawable = this.getResources().getDrawable(R.drawable.ic_launcher);
itemizedoverlay = new MyOverlay(this, drawable);
createMarker();
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
private void createMarker() {
GeoPoint p = mapView.getMapCenter();
OverlayItem overlayitem = new OverlayItem(p, "", "");
itemizedoverlay.addOverlay(overlayitem);
if (itemizedoverlay.size() > 0) {
mapView.getOverlays().add(itemizedoverlay);
}
}
@Override
protected void onResume() {
super.onResume();
myLocationOverlay.enableMyLocation();
myLocationOverlay.enableCompass();
}
@Override
protected void onPause() {
super.onPause();
myLocationOverlay.disableMyLocation();
myLocationOverlay.disableCompass();
}
public class GetMethodEx {
public String getInternetData() throws Exception {
BufferedReader in = null;
String data = "";
//String returnString = null;
// httpGet
try {
HttpClient client = new DefaultHttpClient();
URI website = new URI("http://192.168.1.15/latlonret1.php");
HttpGet request = new HttpGet();
request.setURI(website);
HttpResponse response = client.execute(request);
in = new BufferedReader(new InputStreamReader(response
.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String l = "";
String nl = System.getProperty("line.separator");
while ((l = in.readLine()) != null) {
sb.append(l + nl);
}
in.close();
data = sb.toString();
// return data;
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
}
// parse json data
try {
JSONArray jArray = new JSONArray(data);
for (int i = 0; i < jArray.length(); i++) {
JSONObject json_data = jArray.getJSONObject(i);
JSONObject json_data1 = jArray.getJSONObject(i);
returnString =json_data.getString("lat") + "\n";
returnString1 =json_data1.getString("lon") + "\n";
System.out.println(returnString);
System.out.println(returnString1);
}
Bundle bundle = new Bundle();
bundle.putString("stuff", returnString);
bundle.putString("stuff1", returnString1);
try
{
aa1=Double.valueOf(returnString);
aa2=Double.valueOf(returnString1);
}
catch(Exception e)
{
}
System.out.println(returnString);
System.out.println(returnString1);
}
catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
return returnString;
}
}
}
解决方案
This might be of help. There are also many example on SO too.
这篇关于在Android中的谷歌地图上绘制多个标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!