0

如果我的 json 的格式如下所示,则带有地址和纬度/经度的商店

[{"StoreId":"50","StoreTitle":"Pocket 

Docket","StoreLogo":"","StoreTradingName":"Pocket 

Docket","StoreWebsite":"http:\/

\/pocketdocket.com.au","Address":{"Address1":"367 

","Address2":"George 

St.","City":"Sydney","State":"NSW","ZipCode":"2000","Longitude"

:"151.2284339","Latitude":"-

33.5487607","AreaCode":"2000","Phone":"0280839400"},

和我要显示的mapactivity类如下

public class MapActivity extends com.google.android.maps.MapActivity {
MapView mapView;
private LocationManager mylocmng;
private LocationListener myloclist;
private MapController mympctrl;

private void CenterLocation(GeoPoint centrGeopt){
    mympctrl.animateTo(centrGeopt);
    //Remove title bar
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
};
public void onCreate(Bundle savedInstanceState){




    super.onCreate(savedInstanceState);
    setContentView(R.layout.map);

    mapView=(MapView)findViewById(R.id.mv);
    //mapView.setSatellite(true);
    mapView.setStreetView(true);
    mapView.setBuiltInZoomControls(true);
    mympctrl = mapView.getController();
    mympctrl.setZoom(8);
    mylocmng=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
    myloclist=new MyLocationListener();
    mylocmng.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,myloclist);
    Drawable marker=getResources().getDrawable(android.R.drawable.star_big_on);
     int markerWidth = marker.getIntrinsicWidth();
     int markerHeight = marker.getIntrinsicHeight();
     marker.setBounds(0, markerHeight, markerWidth, 0);
     LocationOverlay myItemizedOverlay = new LocationOverlay(marker);
     mapView.getOverlays().add(myItemizedOverlay);

   /* GeoPoint myPoint1 = new GeoPoint(0*1000000, 0*1000000);
     myItemizedOverlay.addItem(myPoint1, "myPoint1", "myPoint1");
     GeoPoint myPoint2 = new GeoPoint(50*1000000, 50*1000000);
     myItemizedOverlay.addItem(myPoint2, "myPoint2", "myPoint2");
    //Get the current location in start-up
      GeoPoint initGeoPoint = new GeoPoint(
       (int)(mylocmng.getLastKnownLocation(
        LocationManager.GPS_PROVIDER)
        .getLatitude()*1000000),
       (int)(mylocmng.getLastKnownLocation(
        LocationManager.GPS_PROVIDER)
        .getLongitude()*1000000));
       CenterLocation(initGeoPoint);*/



       ImageButton bcknrby=(ImageButton)findViewById(R.id.nearbybuttn);
        bcknrby.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent myint = new Intent(MapActivity.this,NearBy.class);
                startActivity(myint);
            }

        });
     }
private class MyLocationListener implements LocationListener{

    @Override
    public void onLocationChanged(Location argLocation) {
        // TODO Auto-generated method stub
        Drawable marker=getResources().getDrawable(android.R.drawable.star_big_on);
         int markerWidth = marker.getIntrinsicWidth();
         int markerHeight = marker.getIntrinsicHeight();
         marker.setBounds(0, markerHeight, markerWidth, 0);
         LocationOverlay myItemizedOverlay = new LocationOverlay(marker);
         mapView.getOverlays().add(myItemizedOverlay);
        GeoPoint myGeoPoint = new GeoPoint(
                (int)(argLocation.getLatitude()*1000000),
                (int)(argLocation.getLongitude()*1000000));
               myItemizedOverlay.addItem(myGeoPoint,"myPoint1", "myPoint2");
               CenterLocation(myGeoPoint);
    }

    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub
         Toast.makeText( getApplicationContext(), "Gps Disabled", Toast.LENGTH_LONG ).show();
    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub
         Toast.makeText( getApplicationContext(), "Gps Enabled", Toast.LENGTH_SHORT ).show();
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }}





@Override
protected boolean isRouteDisplayed() {
    return false;
}

}

如何将坐标从 json 加载到地图视图??????任何帮助将不胜感激。

这是我的解析代码

try{JSONArray myID=j2.getJSONArray("stores");
        for(int i=0;i<myID.length();i++){
            Log.v("state","json address being read");
            JSONObject j3= myID.getJSONObject(i);
            String name = j3.getString("Address");

            Log.v("Address",name);
4

2 回答 2

2

您可以从这些链接中学习 json 解析。这些链接中显示的非常简单的示例。

http://www.technotalkative.com/android-json-parsing/

http://www.androidcompetencycenter.com/2009/10/json-parsing-in-android/

http://www.androidhive.info/2012/01/android-json-parsing-tutorial/

您可以通过解析 JSON 来获取纬度和经度,然后简单地将这些值传递给您的GeoPoint myGeoPoint.

编辑

 String str = "{\"error\":false,\"totalresults\":6,\"stores\":" +               
                "[{" +
                "\"StoreId\":\"50\",\"StoreTitle\":\"Pocket Docket\",\"StoreLogo\":\"\",\"StoreTradingName\":\"Pocket Docket\"," +
                "\"StoreWebsite\":\"http:\\\\pocketdocket.com.au\"," +
                "\"Address\":" +
                "{\"Address1\":\"367 \",\"Address2\":\"George St.\"," +
                "\"City\":\"Sydney\",\"State\":\"NSW\",\"ZipCode\":\"2000\",\"Longitude\":\"151.2284339\",\"Latitude\":\"-33.5487607\"," +
                "\"AreaCode\":\"2000\",\"Phone\":\"0280839400\"}}]}";

        try 
        {
             jsonObject = new JSONObject(str);
             JSONArray arr_start = jsonObject.getJSONArray("stores");
                JSONObject obj_arr = arr_start.getJSONObject(0);
                JSONObject arr_address = obj_arr.getJSONObject("Address");
                String lat = arr_address.getString("Latitude");
                String Longi = arr_address.getString("Longitude");
        }
        catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

希望这会对您有所帮助,如果对您有帮助,请不要忘记接受它。

谢谢...

于 2012-04-04T11:53:17.957 回答
0

按照 MapView 教程http://developer.android.com/guide/tutorials/views/hello-mapview.html中的描述创建 OverlayItem

于 2012-04-04T11:24:27.037 回答