Issue
If my json is of the format as given below for a store with its addresses and lat/long
[{"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"},
and my mapactivity class to display that is as follows
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;
}
}
how do I load the coordinates from json to the mapview????? any help would be greatly appreciated.
this is my code for parsing
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);
Solution
You can learn json parsing from these links. Very simple example shown in these links.
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/
You can get latitude and longitude by parse JSON, then simple pass those values to your GeoPoint myGeoPoint
.
EDIT
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();
}
Hope this will help you and Don't forgot to accept it if it is helpful to you.
Thanks...
Answered By - Never Quit
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.