0

I have a address name , i want to get the Latitude and Longitude from my app , i reference some tutorial , i still can't get it successfully in the end.

The log value is empty.

What step i miss it ? any help would be grateful,thanks.

global variable:

double editLatitude, editLongitude;

my Geocoder:

 Geocoder coder = new Geocoder(getActivity());
            List<Address> address;

            try {
                address = coder.getFromLocationName("Las Vegas", 5);
                if (address == null) {
                    return null;
                }
                Address location = address.get(0);
                editLatitude = location.getLatitude();
                editLongitude = location.getLongitude();
                Log.d("editLatitude", editLatitude + ">>>>>>>>>>>>>>>>>>");
                Log.d("editLongitude", editLongitude + ">>>>>>>>>>>>>>>>>>");
            } catch (IOException ex) {
                ex.printStackTrace();
            }

i set the latitude and longitude over here:

 @Override
        public void onLocationChanged(Location location) {
            mLastLocation = location;
            if (mCurrLocationMarker != null) {
                mCurrLocationMarker.remove();
            }

            //Place current location marker
            //LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
            //LatLng latLng = new LatLng(22.751262, 121.140801);
//---------------------------------------------
            LatLng latLng = new LatLng(editLatitude, editLongitude);
            MarkerOptions markerOptions = new MarkerOptions();
            markerOptions.position(latLng);
            markerOptions.title("Current Position");
            markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
            mCurrLocationMarker = mGoogleMap.addMarker(markerOptions);

            //move map camera
            mGoogleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
            mGoogleMap.animateCamera(CameraUpdateFactory.zoomTo(16));

            //optionally, stop location updates if only current location is needed
            if (mGoogleApiClient != null) {
                LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
            }
        }
Sunil Sunny
  • 3,949
  • 4
  • 23
  • 53
Morton
  • 5,380
  • 18
  • 63
  • 118
  • Possible duplicate of [Get latitude,longitude from address in Android](http://stackoverflow.com/questions/17835426/get-latitude-longitude-from-address-in-android) – Arpan24x7 Jan 25 '17 at 06:50
  • so some devices will not support my function ? my device is Huawei mate 8 – Morton Jan 25 '17 at 07:09
  • 1
    it may possible there is device issue. https://support.strava.com/hc/en-us/articles/216942547-How-to-fix-GPS-recording-issues-on-Huawei-phones – Arpan24x7 Jan 25 '17 at 07:28
  • thanks for sharing the link , but if my app was used for everyone , obviously not a solution. – Morton Jan 25 '17 at 07:48
  • have you check iin other device – Arpan24x7 Jan 25 '17 at 09:23

2 Answers2

1

create a method that returns a JSONObject with the response of the HTTP Call like following

public static JSONObject getLocationInfo(String address) {
    StringBuilder stringBuilder = new StringBuilder();
    try {

    address = address.replaceAll(" ","%20");    

    HttpPost httppost = new HttpPost("http://maps.google.com/maps/api/geocode/json?address=" + address + "&sensor=false");
    HttpClient client = new DefaultHttpClient();
    HttpResponse response;
    stringBuilder = new StringBuilder();


        response = client.execute(httppost);
        HttpEntity entity = response.getEntity();
        InputStream stream = entity.getContent();
        int b;
        while ((b = stream.read()) != -1) {
            stringBuilder.append((char) b);
        }
    } catch (ClientProtocolException e) {
    } catch (IOException e) {
    }

    JSONObject jsonObject = new JSONObject();
    try {
        jsonObject = new JSONObject(stringBuilder.toString());
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return jsonObject;
}   

now pass that JSONObject to getLatLong() method like following

public static boolean getLatLong(JSONObject jsonObject) {

    try {

        longitute = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
            .getJSONObject("geometry").getJSONObject("location")
            .getDouble("lng");

        latitude = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
            .getJSONObject("geometry").getJSONObject("location")
            .getDouble("lat");

    } catch (JSONException e) {
        return false;

    }

    return true;
}
Rajesh Panchal
  • 1,140
  • 3
  • 20
  • 39
  • thanks for your help , but what is PincodeVerify.class exactely? – Morton Jan 25 '17 at 07:06
  • @徐博俊 Don't bother about the code just try this url http://maps.googleapis.com/maps/api/geocode/json?address=Las%20Vegas .. Parse the json and you will get the lat and long – Sunil Sunny Jan 25 '17 at 07:24
  • Ok i got you , i will use AsyncTask to complete it , but i found it's not very accurate even i know the address , it shows nothing from json sometimes. – Morton Jan 25 '17 at 07:35
0

try this

double Lat = geocoder.getFromLocationName(address_name, 1).get(0).getLatitude();
double Lon = geocoder.getFromLocationName(address_name, 1).get(0).getLongitude();
Patel Pinkal
  • 8,984
  • 4
  • 28
  • 50