如何把JSON lOutput(纬度和经度)在地图上 [英] How to put JSON lOutput (latitude and longitude) on the map

查看:193
本文介绍了如何把JSON lOutput(纬度和经度)在地图上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个主要活动从我的MySQL解析JSON数据(表追踪:Lattitude和纬度)现在,我想通过这个数据,我MapActivity并显示在谷歌地图。任何帮助是非常AP preciated。谢谢!

I have a main activity which parses the JSON data from my mysql (table tracking:Lattitude and longitude) Now I want to pass this data in to my MapActivity and display on google maps. Any help is highly appreciated. Thanks!

这我JSONactivity

this my JSONactivity

  public class JSONActivity extends Activity{
  private JSONObject jObject;

  private String xResult ="";
  //Seusuaikan url dengan nama domain 
  private String url = "http://10.0.2.2/labiltrack/daftartracking.php";

  @Override
 public void onCreate(Bundle savedInstanceState){
  super.onCreate(savedInstanceState);
  setContentView(R.layout.daftartrack);
  TextView txtResult = (TextView)findViewById(R.id.TextViewResult);
  //url += "?lattitude=" + UserData.getEmail();
  xResult = getRequest(url);
   try {
        parse(txtResult);
    } catch (Exception e) {
        e.printStackTrace();
    }
  }

 private void parse(TextView txtResult) throws Exception {
 // TODO Auto-generated method stub
 jObject = new JSONObject(xResult);
 JSONArray menuitemArray = jObject.getJSONArray("joel");
 String sret="";
 //int j = 0;
 for (int i = 0; i < menuitemArray.length(); i++) {
    sret +=menuitemArray.getJSONObject(i).
            getString("lattitude").toString()+" : ";
    System.out.println(menuitemArray.getJSONObject(i)
            .getString("lattitude").toString());
    System.out.println(menuitemArray.getJSONObject(i).getString(
    "longitude").toString());
    sret +=menuitemArray.getJSONObject(i).getString(
    "lattitude").toString()+"\n";   
    //j=i;
  }txtResult.setText(sret);

 }
 /**
* Method untuk Mengirimkan data keserver
 * event by button login diklik
 *
 * @param view
 */
 private String getRequest(String url) {
 // TODO Auto-generated method stub
 String sret="";
 HttpClient client = new DefaultHttpClient();
 HttpGet request = new HttpGet(url);
 try{
   HttpResponse response = client.execute(request);
   sret =request(response);

  }catch(Exception ex){
    Toast.makeText(this,"jo "+sret, Toast.LENGTH_SHORT).show();
  }
  return sret;
 }

   /**
  * Method untuk Menenrima data dari server
  * @param response
 * @return
  */
private String request(HttpResponse response) {
  // TODO Auto-generated method stub

  String result = "";
  try{
      InputStream in = response.getEntity().getContent();
      BufferedReader reader = new BufferedReader(new InputStreamReader(in));
      StringBuilder str = new StringBuilder();
      String line = null;
      while((line = reader.readLine()) != null){
        str.append(line + "\n");
     }
     in.close();
     result = str.toString();
  }catch(Exception ex){
     result = "Error";
  }
  return result;
  }
  }

和本我mapActivity

and this my mapActivity

public class mapactivity extends MapActivity {
private MapView mapView;
 MapController mc;
GeoPoint p;
//private MyLocationOverlay me = null;

 class MapOverlays extends com.google.android.maps.Overlay
{

  @Override
  public boolean draw (Canvas canvas, MapView mapView, boolean shadow, long when)
    {
    super.draw(canvas, mapView, shadow);

    //translate the geopoint to screen pixels
    Point screenPts = new Point();
    mapView.getProjection().toPixels(p, screenPts);

    //tambah marker
    Bitmap bmp = BitmapFactory.decodeResource(getResources (), R.drawable.pin_red);
    canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);
    //mapView.setSatellite(true);       

    return true;
  }  

   @Override
   public boolean onTouchEvent(MotionEvent event, MapView mapView) 
   {   
     //---when user lifts his finger---
     if (event.getAction() == 1) {                
         GeoPoint p = mapView.getProjection().fromPixels(
             (int) event.getX(),
             (int) event.getY());

         Toast.makeText(getBaseContext(), 
                 p.getLatitudeE6() / 1E6 + "," + 
                 p.getLongitudeE6() /1E6 , 
                 Toast.LENGTH_SHORT).show();
         mc.animateTo(p);

            //geocoding 
         Geocoder geoCoder = new Geocoder(
             getBaseContext(), Locale.getDefault());
         try {
             List<Address> addresses = geoCoder.getFromLocation(
                 p.getLatitudeE6()  / 1E6, 
                 p.getLongitudeE6() / 1E6, 1);

             String add = "";
             if (addresses.size() > 0) 
             {
                 for (int i=0; i<addresses.get(0).getMaxAddressLineIndex(); 
                      i++)
                    add += addresses.get(0).getAddressLine(i) + "\n";
             }

             Toast.makeText(getBaseContext(), add, Toast.LENGTH_SHORT).show();
         }
         catch (IOException e) {                
             e.printStackTrace();
         }   
         return true;
     }
      else                
         return false;
     }      }         

   /** Called when the activity is first created. */
   @SuppressWarnings("deprecation")
 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.mapview1);

 //utk mnampilkan zoom
 mapView = (MapView) findViewById(R.id.mapView);
 LinearLayout zoomLayout = (LinearLayout)findViewById(R.id.zoom);  
 View zoomView = mapView.getZoomControls(); 

    zoomLayout.addView(zoomView, 
        new LinearLayout.LayoutParams(
        LayoutParams.FILL_PARENT, 
        LayoutParams.FILL_PARENT)); 
    mapView.displayZoomControls(true);       

    //menampilkan default peta banda aceh 
    mc = mapView.getController();
    String coordinates[] = {"5.550381", "95.318699"};
    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(14); 
        mapView.invalidate();

        //tambah marker
        MapOverlays mapOverlay = new MapOverlays();
        List<Overlay> listOfOverlays = mapView.getOverlays();
        listOfOverlays.clear();
        listOfOverlays.add(mapOverlay);        

        mapView.invalidate();
        }


   public void btnSatelitClick(View v){
  mapView.setSatellite(true);
  mapView.setStreetView(false);

 }

 public void btnjalanClick (View v){
  mapView.setSatellite(false);
  mapView.setStreetView(true);
  }

          protected boolean isRouteDisplayed() 
          {
              //auto generate method
              return false;
         }

    }

我jsonactivity是从MySQL(场纬度和经度)进入列表视图中获取数据,但现在我想显示的数据在谷歌地图(经度和纬度),我怎么能这样做? 请帮帮我,先谢谢了!

my jsonactivity is get data from mysql (field "latitude" and "longitude") into listview, but now I want to display that data (latitude and longitude) on google map, How Could I do this ? please help me, thanks in advance !

推荐答案

您需要从另一个类获得包:这个类将是你的 mapActivity

You need to get bundle from another class : this class will be for your mapActivity

Bundle b = getIntent().getExtras(); // Getting the Bundle object that pass from another activity
        int SelectedPropertylat = b.getInt("SelectedLat");
        int SelectedPropertylong = b.getInt("SelectedLong");

        String  lattitude = Integer.toString(SelectedPropertylat);          
        String  longertude = Integer.toString(SelectedPropertylong);

        Log.d(lattitude,longertude);

和采取datafrom的MySQL到您的应用程序使用这样的:

And taking datafrom mysql into your apps use this :

try{

    JSONArray  earthquakes = json.getJSONArray("PropertyDetails");

    for(int i=0;i<earthquakes.length();i++){                        

        JSONObject e = earthquakes.getJSONObject(i);
                lat = e.getString("P_Lat");
        lonng = e.getString("P_Long");

然后将其转换纬度和长到像一个字符串:

then convert lat and long into an string like :

lonnng = Integer.parseInt(lonng.toString());
        latt =Integer.parseInt(lat.toString());

然后将数据传递到您的图形页面是这样的:

then pass the data into your mapview like this :

Intent moreDetailsIntent = new Intent(PropertiesDetails.this,mapActivity .class);

            Bundle dataBundle = new Bundle();
            dataBundle.putInt("SelectedLong",lonnng);
            dataBundle.putInt("SelectedLat", latt);
            moreDetailsIntent.putExtras(dataBundle);
            startActivity(moreDetailsIntent);

这篇关于如何把JSON lOutput(纬度和经度)在地图上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆