简体   繁体   中英

how to access current location using google maps in android

I'm doing a project on an app that shows the current location of the user using google maps. I have got to show the map view of the current location of the user. I tried using a link that connects to the google maps to show the map but I can't get to display the current location of the user. Is there any way to show the map without using the API key ? It seemed a bit difficult so I wanted to keep it as simple as possible. I'm new to this so I need all the help I can get. Thanks

If you want to display google maps without API key, You want to go to google maps website from your app. You can provide latitude and longitude directly in that link. Then It will show that particular location.

Example: http://maps.google.com/?q=25.184880830029137,55.34197383056186

And, No need of map for getting current location. Try this:

public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    LocationListener ll = new mylocationlistener();
    lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 1, ll);
//You can use GPS provider or NETWORK provide  }

private class mylocationlistener implements LocationListener {
    public void onLocationChanged(Location location) {    
         try { 
        if (location != null) {
        Log.d("LATITUDE", location.getLatitude() + "");
        Log.d("LONGITUDE", location.getLongitude() + "");
//Go to maps website from here. Your current location will shown in map without API key.
// Your URL : http://maps.google.com/?q=location.getLatitude(),location.getLongitude()
             }}catch (Exception e) {
                Log.e("Error", e.toString());
            } 
    } 
    public void onProviderDisabled(String provider) {
    Toast.makeText(getApplicationContext(),"Provider Disabled",Toast.LENGTH_LONG).show();
    }    
    public void onProviderEnabled(String provider) {
    Toast.makeText(getApplicationContext(),"Provider Enabled",Toast.LENGTH_LONG).show();
    }
    public void onStatusChanged(String provider, int status, Bundle extras) {
    Toast.makeText(getApplicationContext(),"Status Changed",Toast.LENGTH_LONG).show();
    }
}

You cannot display Google Maps in your app without using the Google Maps API . It isn't even that difficult

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