简体   繁体   English

从 json 解析纬度和经度后,如何在 Google Map 中放置标记?

[英]How to place marker in Google Map after parsing lattitude and longitude from json?

I have parse json file which contain more than hundred location name, there latitude and longitude.我已经解析了 json 文件,其中包含一百多个位置名称,有纬度和经度。
Up to now i am able to get location name, lattitude and longitude.到目前为止,我能够获取位置名称、纬度和经度。

I want to plot these all values in google map and place a marker for each of them.我想 plot 这些所有值在谷歌 map 并为每个值放置一个标记。

Also I want to show the location name which i got from parsing JSON when user touch the marker placed in google map.当用户触摸放置在谷歌 map 中的标记时,我还想显示我从解析 JSON 获得的位置名称。

I solve it guys.我解决了伙计们。 see below:见下文:

Update: main.xml更新: main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

 <com.google.android.maps.MapView 
    android:id="@+id/mapView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:enabled="true"
    android:clickable="true"
    android:apiKey="0hlQp-ys3VmVGNLoJ0aGYQhDmBH5KC-ZQmc3yNA"
    />

<LinearLayout
    android:id="@+id/zoom"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true" />

 </RelativeLayout>

ShowMapActivity.java ShowMapActivity.java

public class ShowMapActivity extends MapActivity {

private static String url = "http://10.0.2.2/test/myfile.php";
JSONArray root = null;
private static final String TAG_ROOT = "root";
private static final String TAG_LATTITUDE = "lattitude";
private static final String TAG_LONGITUDE = "longitude";
private static final String TAG_NAME="name";

MapController mc;
GeoPoint p;

double lattitudeValue;
double longitudeValue;
String lattitude;
String longitude;
String name;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // showing MapView
    MapView mapView = (MapView) findViewById(R.id.mapView);
    LinearLayout zoomLayout = (LinearLayout) findViewById(R.id.zoom);
    View zoomView = mapView.getZoomControls();
    zoomLayout.addView(zoomView, new LinearLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    mapView.displayZoomControls(true);
    mc = mapView.getController();
    String coordinates[] = { "53.5146152", "-2.2857034" };
    double lat = Double.parseDouble(coordinates[0]);
    double lng = Double.parseDouble(coordinates[1]);
    p = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
    mc.animateTo(p);
    mc.setZoom(10);
    mapView.invalidate();


    // Parsing JSON value and Read location name, lattitude, longitude from JSON


    JSONParser jParser = new JSONParser();

    JSONObject json = jParser.getJSONFromUrl(url);
    System.out.println("Testingggg..." + json.length());

    try {

        root = json.getJSONArray(TAG_ROOT);
        for (int i = 0; i < root.length(); i++) {
            JSONObject c = root.getJSONObject(i);
            name=c.getString(TAG_NAME);   //Getting location name
            lattitude = c.getString(TAG_LATTITUDE); //Getting lattitude value in string
            longitude = c.getString(TAG_LONGITUDE); //Getting longitude value in string
            lattitudeValue = Double.parseDouble(lattitude); //converting string lattitude value to double
            longitudeValue=Double.parseDouble(longitude); //converting string longitude value to double



                List<Overlay> mapOverlays = mapView.getOverlays();
            Drawable drawable = this.getResources().getDrawable(
                    R.drawable.locationmarker);
            HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(
                    drawable, this);
            GeoPoint point = new GeoPoint((int) (lattitudeValue * 1E6),
                    (int) (longitudeValue * 1E6));
            OverlayItem overlayitem = new OverlayItem(point, name, "I'm in"
                    + name);

            itemizedoverlay.addOverlay(overlayitem);
            mapOverlays.add(itemizedoverlay);



        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

@Override
protected boolean isRouteDisplayed() {
    // TODO Auto-generated method stub
    return false;
}
}

CustomItemizedOverlay.java CustomItemizedOverlay.java

public class CustomItemizedOverlay extends ItemizedOverlay<OverlayItem> {

private final ArrayList<OverlayItem> mapOverlays = new ArrayList<OverlayItem>();

private Context context;

public CustomItemizedOverlay(Drawable defaultMarker) {
    super(boundCenterBottom(defaultMarker));
}

public CustomItemizedOverlay(Drawable defaultMarker, Context context) {
    this(defaultMarker);
    this.context = context;
}

@Override
protected OverlayItem createItem(int i) {
    return mapOverlays.get(i);
}

@Override
public int size() {
    return mapOverlays.size();
}

public void addOverlay(OverlayItem overlay) {
    mapOverlays.add(overlay);
    this.populate();
}

} }

HelloItemizedOverlay.java HelloItemizedOverlay.java

public class HelloItemizedOverlay extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
private Context mContext;

public HelloItemizedOverlay(Drawable defaultMarker, Context context) {
    super(boundCenterBottom(defaultMarker));
    mContext = context;
}

public void addOverlay(OverlayItem overlay) {
    mOverlays.add(overlay);
    populate();
}

@Override
protected OverlayItem createItem(int i) {
    return mOverlays.get(i);
}

@Override
public int size() {
    return mOverlays.size();
}

@Override
protected boolean onTap(int index) {
    OverlayItem item = mOverlays.get(index);
    AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
    dialog.setTitle(item.getTitle());
    dialog.setMessage(item.getSnippet());
    dialog.show();
    return true;
}
 }
Drawable srcdrawable = getApplicationContext().getResources().getDrawable(R.drawable.pin_blue);
CustomItemizedOverlay srcitemizedOverlay = new CustomItemizedOverlay(srcdrawable, getApplicationContext());
forloop(setoflocations){
GeoPoint srcpoint = new GeoPoint((int)( Double.parseDouble(lat) * 1E6),(int)( Double.parseDouble(lng)* 1E6));
OverlayItem srcoverlayitem = new OverlayItem(srcpoint, "Hello!", "This is your Location.");
if(srcitemizedOverlay!=null && mapController!=null){
srcitemizedOverlay.addOverlay(overlayitem);
mapController.animateTo(point);
animatePoint = point;
}
}
mapView.getOverlays().clear();
mapView.getOverlays().add(srcitemizedOverlay);

Use the above code in oncreate() after you get set of location also below is the below CustomItemizedOverlay.java class在获得位置集后,在 oncreate() 中使用上面的代码,下面是 CustomItemizedOverlay.java class

public class CustomItemizedOverlay extends ItemizedOverlay<OverlayItem> {

    private final ArrayList<OverlayItem> mapOverlays = new ArrayList<OverlayItem>();

    private Context context;

    public CustomItemizedOverlay(Drawable defaultMarker) {
        super(boundCenterBottom(defaultMarker));
    }

    public CustomItemizedOverlay(Drawable defaultMarker, Context context) {
        this(defaultMarker);
        this.context = context;
    }

    @Override
    protected OverlayItem createItem(int i) {
        return mapOverlays.get(i);
    }

    @Override
    public int size() {
        return mapOverlays.size();
    }

    public void addOverlay(OverlayItem overlay) {
        mapOverlays.add(overlay);
        this.populate();
    }

}

Refer this LINK .请参阅此链接

Make an overlay to show the marker..制作叠加层以显示标记..

check this out link检查这个链接

Hope it will help you..希望它能帮助你..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Android Google Map V2上放置标记/绘制经度和​​纬度 - How to place marker/ plot Longitude and latitude on android google map v2 在Android中解析后,如何显示从xml网址获取的Google地图,纬度和经度中的标记? - how to show markers in google map ,latitude and longitude obtained from xml url after parsing in android? 特定地点的Google地图经度和纬度 - Google Map longitude and latitude from particular place 我想通过从json获取经度和纬度在Google地图上显示标记 - i want to show marker on google map by getting longitude and latitude from json 从 Google 地图上的标记获取地名 - Getting the place name from a marker on Google Map JSON解析错误Google Map Place API - JSON parsing error google map place api 如何在谷歌地图上的屏幕中心放置标记 - How to place a Marker on center of the screen on google map 如何从谷歌地图API响应的JSON获取纬度/经度 - How to get Latitude/Longitude from JSON responsed by google map apis 如何通过在线json在Google地图上标记纬度和经度 - how to mark latitude and longitude on google map from an online json 当我从短信中接收经度和纬度时,如何在Google地图中添加标记 - How to add marker in google map when i recieve longitude and latitude from sms
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM