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 に答える