2016-04-22 14 views
0

マップを使用してアプリケーションを作成していて、問題が発生しました。 私は自分のFragmentとAutoCompleteTextViewにmapViewを持っています。オートコンプリートから都市を選択すると、マップ上にマーカーとして表示されます。Google Geocoding API jsonからLatLngデータを取得するにはどうすればよいですか?

ここはコードであり、実際のSamsung Galaxy S3とGalaxy S6エミュレータでうまくいきますが、Meizu MX5では動作しません。 ジオコーダーはすべてのデバイスで正常に動作しないため、GoogleジオコーディングAPIを使用したソリューションがあります。私はJsonの答えを得ましたが、LatLangとFeatureNameをJSONから取得する方法はわかりません。 JSONの

例:http://maps.google.com/maps/api/geocode/json?address=London&sensor=true

私は明確な、アンドロイドへ新しい気の毒そうでない場合はすべてのものです。

atvFrom.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      locationFrom = atvFrom.getText().toString(); 
      List<Address> addressListFrom = null; 
      Geocoder geocoder = new Geocoder(getActivity()); 

      try { 
       if (markerFrom[0] != null) {markerFrom[0].remove();} 
       addressListFrom = geocoder.getFromLocationName(locationFrom, 1); 

       if (addressListFrom != null) { 
        Address addressFrom = addressListFrom.get(0); 
        latLngFrom[0] = new LatLng(addressFrom.getLatitude(), addressFrom.getLongitude()); 
        tripFrom = addressListFrom.get(0).getFeatureName(); 

        markerFrom[0] = googleMap.addMarker(new MarkerOptions().position(latLngFrom[0]).title("From")); 
        googleMap.animateCamera(CameraUpdateFactory.newLatLng(latLngFrom[0])); 

        if (markerTo[0]!=null) { 
         if (line[0] !=null) {line[0].remove();} 
         line[0] = googleMap.addPolyline(new PolylineOptions().add(latLngFrom[0], latLngTo[0]).width(5).color(Color.BLUE)); 
        } 

       } else { 
        MyGeocoder myGeocoder = new MyGeocoder(FragmentMap.this); 
        myGeocoder.execute(locationFrom); 
        try { 
         JSONObject jObj = new JSONObject(myGeocoder.jsonResult); 
         String Status = jObj.getString("status"); 
         if (Status.equalsIgnoreCase("OK")) { 
          JSONArray Results = jObj.getJSONArray("results"); 
          // how to get LatLng and FeatureName from json? 
          // dont know what to do here 

         } 
        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 
       } 

      } catch (IOException e) { 
       //e.printStackTrace(); 
       Toast.makeText(getActivity(), "Error", Toast.LENGTH_SHORT).show(); 
      } 
     } 
    }); 

答えて

0

locationに到達するには、Jsonオブジェクトをナビゲートする必要があります。これは、あなたが

try { 
    JSONObject jObj = new JSONObject(myGeocoder.jsonResult); 
    String Status = jObj.getString("status"); 
    if (Status.equalsIgnoreCase("OK")) { 
     JSONArray results = jObj.getJSONArray("results"); 
     JSONObject item = results.getJSONObject(0);//read first item of results 
     JSONObject geometry = item.getJSONObject("geometry");//location is inside geometry object 
     JSONObject location = geometry.getJSONObject("location"); 
     double latitude = location.getDouble("lat"); 
     double longitude = location.getDouble("lng"); 
     //or 
     LatLng latLng = new LatLng(latitude, longitude); 
    } 
} catch (JSONException e) { 
    e.printStackTrace(); 
} 

を行う必要があるとジオコーダ実装が存在するかどうかを判断するために

利用isPresent()メソッドメソッドの下に呼び出して、デバイス上ジオコーダ existanceをチェックする方法です。

if(Geocoder.isPresent()){ 
    //exists 
} 

happyCoding。

関連する問題