Skip to content Skip to sidebar Skip to footer

Get Location Name By Passing Latitude And Longitude In Android

I have written following code for getting location name : UseGpsActivity.java public class UseGpsActivity extends Activity { /** Called when the activity is first created. */

Solution 1:

I used Following code and now I am able to get the name of location :

publicclassGPSLocatorActivityextendsMapActivity {
    private MapView mapView;
    private MapController mapController;

    private LocationManager locationManager;
    private LocationListener locationListener;

    @OverridepublicvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);    

        locationListener = newGPSLocationListener();

        locationManager.requestLocationUpdates(
            LocationManager.GPS_PROVIDER, 
            0, 
            0, 
            locationListener);

        mapView = (MapView) findViewById(R.id.mapView);

        // enable Street view by default
        mapView.setStreetView(true);

        // enable to show Satellite view// mapView.setSatellite(true);// enable to show Traffic on map// mapView.setTraffic(true);
        mapView.setBuiltInZoomControls(true);

        mapController = mapView.getController();
        mapController.setZoom(16); 
    }

    @OverrideprotectedbooleanisRouteDisplayed() {
        returnfalse;
    }

    privateclassGPSLocationListenerimplementsLocationListener 
    {
        @OverridepublicvoidonLocationChanged(Location location) {
            if (location != null) {
                GeoPointpoint=newGeoPoint(
                        (int) (location.getLatitude() * 1E6), 
                        (int) (location.getLongitude() * 1E6));

                /* Toast.makeText(getBaseContext(), 
                        "Latitude: " + location.getLatitude() + 
                        " Longitude: " + location.getLongitude(), 
                        Toast.LENGTH_SHORT).show();*/

                mapController.animateTo(point);
                mapController.setZoom(16);

                // add markerMapOverlaymapOverlay=newMapOverlay();
                mapOverlay.setPointToDraw(point);
                List<Overlay> listOfOverlays = mapView.getOverlays();
                listOfOverlays.clear();
                listOfOverlays.add(mapOverlay);
                Stringaddress= ConvertPointToLocation(point);
                Toast.makeText(getBaseContext(), address, Toast.LENGTH_SHORT).show();
                mapView.invalidate();
            }
        }

        public String ConvertPointToLocation(GeoPoint point) {   
            Stringaddress="";
            GeocodergeoCoder=newGeocoder(
                    getBaseContext(), Locale.getDefault());
            try {
                List<Address> addresses = geoCoder.getFromLocation(
                    point.getLatitudeE6()  / 1E6, 
                    point.getLongitudeE6() / 1E6, 1);

                if (addresses.size() > 0) {
                    for (intindex=0; index < addresses.get(0).getMaxAddressLineIndex(); index++)
                        address += addresses.get(0).getAddressLine(index) + " ";
                }
            }
            catch (IOException e) {                
                e.printStackTrace();
            }   

            return address;
        } 

        @OverridepublicvoidonProviderDisabled(String provider) {
        }

        @OverridepublicvoidonProviderEnabled(String provider) {
        }

        @OverridepublicvoidonStatusChanged(String provider, int status, Bundle extras) {
        }
    }

    classMapOverlayextendsOverlay
    {
        private GeoPoint pointToDraw;

        publicvoidsetPointToDraw(GeoPoint point) {
            pointToDraw = point;
        }

        public GeoPoint getPointToDraw() {
            return pointToDraw;
        }

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

            // convert point to pixelsPointscreenPts=newPoint();
            mapView.getProjection().toPixels(pointToDraw, screenPts);

            // add markerBitmapbmp= BitmapFactory.decodeResource(getResources(), R.drawable.red);
            canvas.drawBitmap(bmp, screenPts.x, screenPts.y - 24, null); // 24 is the height of image        returntrue;
        }
    } 
}

Solution 2:

Use following code to get address

String url = "http://maps.googleapis.com/maps/api/geocode/json?latlng="+ lat+ ","+ lon+ "&sensor=true";
            String res = IOUtils.MakeRequest(url);
            Log.e("result", res);
            if (res != null && !res.equalsIgnoreCase("")) {
                JSONObject jObject = newJSONObject(res);
//              if (jObject.getString("status").equalsIgnoreCase("ok")) {JSONArray jArray = jObject.getJSONArray("results");
                    if(jArray.length() > 0){
                        JSONObject geo = jArray.getJSONObject(0);
                        address = geo.getString("formatted_address");
                        String[] splite = address.split(",");
                        if(splite.length > 3){
                            String temp = splite[2];
                            temp = temp.substring(0,3);
                            address = "";
                            address+=splite[0]+","+splite[1]+","+temp+","+splite[3];
                        }
                        else
                        {
                            address="No Address";
                        }
                        Log.e("address", address);


                    }

//              }

Post a Comment for "Get Location Name By Passing Latitude And Longitude In Android"