简体   繁体   中英

How to get LatLng data from Google Geocoding API json?

I am creating an application with map and faced with a problem. I have a mapView in my Fragment and AutoCompleteTextView. When I pick any city from the AutoComplete, it should appear as a marker on the map.

Here is the code and it is working well on real Samsung Galaxy S3 and Galaxy S6 emulator, but not work on Meizu MX5. As I found, Geocoder does not work properly on every device, so there is a solution with Google Geocoding API. I got the Json answer, but don't know how to get LatLang and FeatureName from JSON.

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

I am a new to Android, sorry if not everything clear.

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();
            }
        }
    });

You need to navigate through Json Objects to reach location . this is how you need to do

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();
}

And to check Geocoder existance on devices, call below method

Use the isPresent() method to determine whether a Geocoder implementation exists.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM