-1

I want to get latitude and longitude of my location and after, i convert there to Location. but i dont know How should i get latitude and longitude of my location?

Location.java:

public class Location extends MapActivity {

MapView map;
MapController controller;
GeoPoint mygeo;
GeoPoint searchgeo;

@Override
public void onCreate(Bundle onSavedInstance){
    super.onCreate(onSavedInstance);
    setContentView(R.layout.location);

    map=(MapView)findViewById(R.id.mapView);
    LinearLayout zoomLayout=(LinearLayout)findViewById(R.id.zoom);
    View zoomView=map.getZoomControls();
    zoomLayout.addView(zoomView,new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,Gravity.BOTTOM+Gravity.CENTER_HORIZONTAL));

    map.displayZoomControls(true);
    //
    controller=map.getController();
    map.setSatellite(true);

    initMyLocation();


}
private void initMyLocation(){
    final MyLocationOverlay overlay=new MyLocationOverlay(this,map);
    overlay.enableMyLocation();
    overlay.enableCompass();
    overlay.runOnFirstFix(new Runnable(){
        public void run(){
            controller.setZoom(8);
            controller.animateTo(overlay.getMyLocation());
        }
    });
    map.getOverlays().add(overlay);

}


@Override 
public boolean isRouteDisplayed(){
    return false;
}
 }

Thanks

Cheers

4 Answers4

0

Check this out. It shows exactly how to obtain your current location, and getting its lat and long GPS not update location after close and reopen app on android

please note you have to include the necessary permissions in the manifest for this to work

as seen here https://stackoverflow.com/a/3380641/975959

Community
  • 1
  • 1
La bla bla
  • 8,558
  • 13
  • 60
  • 109
0

Use Location Listener to get your current location (Latitude and Longitude).

This to call Location Listener

    LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
    LocationListener mlocListener = new MyLocationListener();  
    mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener); 

You can get your latitude and longitude in this class

    public class MyLocationListener implements LocationListener

    {

    @Override 
    public void onLocationChanged(Location loc) 
    {        
     double longitude =  loc.getLongitude(); 
     double latitude = loc.getLatitude();  
    }  
    @Override 
    public void onProviderDisabled(String provider) 
    { 
       Toast.makeText( getApplicationContext(),"Enable GPS",Toast.LENGTH_SHORT ).show();
       Intent enablegps = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);    
       startActivity(enablegps);
    }

    @Override 
    public void onProviderEnabled(String provider) 
    { 
    Toast.makeText( getApplicationContext(), "Gps Enabled", Toast.LENGTH_SHORT).show();  
    }

    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) 
    {} }

Hope it helps.

vinothp
  • 9,939
  • 19
  • 61
  • 103
0

Especially In your case do this in initMyLocation()

double latitude = (overlay.getMyLocation().getLatitudeE6())/ 1e6;

double longitude = (overlay.getMyLocation().getLongitudeE6())/ 1e6;
Kalpesh Lakhani
  • 1,003
  • 14
  • 28
0

Please follow the step below :

Step 1 : Create main.xml like below with a button. when you press it should Toast you current Location.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<Button
 android:id="@+id/retrieve_location_button"
 android:text="Retrieve Location"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 />
</LinearLayout>

step 2 : Create an Activity like below :

package com.xxx.yyy.zzz;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class LbsGeocodingActivity extends Activity {

        private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in Meters
        private static final long MINIMUM_TIME_BETWEEN_UPDATES = 1000; // in Milliseconds

        protected LocationManager locationManager;

        protected Button retrieveLocationButton;

        @Override
        public void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            retrieveLocationButton = (Button) findViewById(R.id.retrieve_location_button);

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

            locationManager.requestLocationUpdates(
                    LocationManager.GPS_PROVIDER,
                    MINIMUM_TIME_BETWEEN_UPDATES,
                    MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
                    new MyLocationListener()
            );

        retrieveLocationButton.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    showCurrentLocation();
                }
        });       

        }   

        protected void showCurrentLocation() {

            Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

            if (location != null) {
                String message = String.format(
                        "Current Location \n Longitude: %1$s \n Latitude: %2$s",
                        location.getLongitude(), location.getLatitude()
                );
                Toast.makeText(LbsGeocodingActivity.this, message,
                        Toast.LENGTH_LONG).show();
            }

        }  

        private class MyLocationListener implements LocationListener {

            public void onLocationChanged(Location location) {
                String message = String.format(
                        "New Location \n Longitude: %1$s \n Latitude: %2$s",
                        location.getLongitude(), location.getLatitude()
                );
                Toast.makeText(LbsGeocodingActivity.this, message, Toast.LENGTH_LONG).show();
            }

            public void onStatusChanged(String s, int i, Bundle b) {
                Toast.makeText(LbsGeocodingActivity.this, "Provider status changed",
                        Toast.LENGTH_LONG).show();
            }

            public void onProviderDisabled(String s) {
                Toast.makeText(LbsGeocodingActivity.this,
                        "Provider disabled by the user. GPS turned off",
                        Toast.LENGTH_LONG).show();
            }

            public void onProviderEnabled(String s) {
                Toast.makeText(LbsGeocodingActivity.this,
                        "Provider enabled by the user. GPS turned on",
                        Toast.LENGTH_LONG).show();
            }

        }

    }

Step 3 : Create AndroidManifest.xml with following three permissions :

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

When you run this app you will find a button on the screen of android device. When you press this button then you will see Toast message with latitude and longitude of your current gps location.

gnat
  • 6,213
  • 108
  • 53
  • 73