1

我有解析 json 文件,其中包含一百多个位置名称,有纬度和经度。
到目前为止,我能够获得位置名称、纬度和经度。

我想在谷歌地图中绘制这些所有值并为每个值放置一个标记。

我还想显示当用户触摸放置在谷歌地图中的标记时我从解析 JSON 中获得的位置名称。

我解决了伙计们。见下文:

更新: 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

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

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

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;
}
 }
4

2 回答 2

2
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);

在您获得一组位置后,在 oncreate() 中使用上面的代码,下面是下面的 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();
    }

}

请参阅此链接

于 2012-04-04T10:52:48.400 回答
1

进行叠加以显示标记..

看看这个链接

希望对你有帮助。。

于 2012-04-04T10:53:28.927 回答