Skip to content Skip to sidebar Skip to footer

Android Google Maps Api Location

I've googled around a bit, and still can't find a definite answer to this. What is the current 'best' or 'recommended' way of using Google Maps in Android? I want my application to

Solution 1:

I am using this code:

//get locationManager object from System Service LOCATION_SERVICELocationManagerlocationManager= (LocationManager) getSystemService(LOCATION_SERVICE);


            Criteriacriteria=newCriteria();

            Stringprovider= locationManager.getBestProvider(criteria, true);

            LocationmyLocation= locationManager.getLastKnownLocation(provider);

            map.setMapType(GoogleMap.MAP_TYPE_NORMAL);

            doublelatitude= myLocation.getLatitude();
            doublelongitude= myLocation.getLongitude();

            LatLnglatLng=newLatLng(latitude, longitude);


            map.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 10));

Solution 2:

You can show the user's current location with the blue dot by enabling the My Location layer. Simply call the following method and the blue dot showing the user's current location will be displayed on the map:

mMap.setMyLocationEnabled(true);

To get location updates you should setup a LocationListener using a LocationClient. When you implement the LocationListener interface you will override the onLocationChanged method which will provide you with location updates. When you request location updates using a LocationClient you create a LocationRequest to specify how frequently you want location updates.

There is an excellent tutorial at the Android Developers site for making you app location aware.

Solution 3:

You have to work with Location Listener. It will solve all you want. Use this code. I tried to explain all things in comments.

import android.location.Location;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks;
import com.google.android.gms.common.GooglePlayServicesClient.OnConnectionFailedListener;
import com.google.android.gms.location.LocationClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;

publicclassYourActivityextendsFragmentActivityimplementsLocationListener, ConnectionCallbacks, OnConnectionFailedListener{

    privateGoogleMap mMap;
    privateLocationClient mLocationClient;
    privatestatic final LocationRequestREQUEST = LocationRequest.create()
            .setInterval(1000)         // 1 second - interval between requests
            .setFastestInterval(16)    // 60fps
            .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

    @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.your_map);

    }

    @OverrideprotectedvoidonResume() {
        super.onResume();
        setUpMapIfNeeded();
        setUpLocationClientIfNeeded();
        //.. and connect to client
        mLocationClient.connect();

    }

    @OverridepublicvoidonPause() {
        super.onPause();
        // if you was connected to client you have to disconnectif (mLocationClient != null) {
            mLocationClient.disconnect();
        }
    }

    privatevoidsetUpMapIfNeeded() {
        // if map did not install yetif (mMap == null) {
            // install it from fragment
            mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                    .getMap();
            // if installation successif (mMap != null) {
                // init all settings of mapinit();
            }
        }
    }

    //if client was not created - create itprivatevoidsetUpLocationClientIfNeeded() {
        if (mLocationClient == null) {
            mLocationClient = newLocationClient(
                    getApplicationContext(),
                    this, 
                    this);
        }
    }

    privatevoidinit() {
        // settings of your map
        mMap.setBuildingsEnabled(true);
        mMap.setMyLocationEnabled(true);
        mMap.getUiSettings().setCompassEnabled(true);
        mMap.getUiSettings().setMyLocationButtonEnabled(true);

    }

    // when you get your current location - set it on center of screen@OverridepublicvoidonLocationChanged(Location location) {

        CameraPosition cameraPosition = newCameraPosition.Builder()
        .target(newLatLng(location.getLatitude(), location.getLongitude())).zoom(9).bearing(0).tilt(0).build();
        CameraUpdate cameraUpdate = CameraUpdateFactory
        .newCameraPosition(cameraPosition);
        mMap.animateCamera(cameraUpdate);

        //Be careful, if want to show current location only once(after starting map) use this line//but you don't need it in your task!
        mLocationClient.removeLocationUpdates(this);
    }

    // after connection to client we will get new location from LocationListener //with settings of LocationRequest@OverridepublicvoidonConnected(Bundle arg0) {
        mLocationClient.requestLocationUpdates(
                REQUEST, //settings for this request described in "LocationRequest REQUEST" at the beginningthis);
    }

    @OverridepublicvoidonDisconnected() {

    }

    @OverridepublicvoidonConnectionFailed(ConnectionResult arg0) {

    }
}

Solution 4:

I was looking for it last 2 days. Answering your question:

What is the current "best" or "recommended" way of using Google Maps in Android?

Today you have 2 options:

  1. Use new/latest Location Service API (like google recommend). But there is a "little" problem: The official guide Location APIs is outdate. I mean, if you follow their tutorial Making Your App Location-Aware you will find that they are using LocationClient which is deprecated. After 2 days looking for the "new" way to do it I found this nice answer Android LocationClient class is deprecated but used in documentation.
  2. Use LocationManager. This second one is the most used. You can find how to use it here and here.

As far as I have seen both works fine but if you ask to me which one I will use, I will choose the one Google recommend: #1.

Here is what they say about it:

The Google Location Services API, part of Google Play Services, provides a more powerful, high-level framework that automatically handles location providers, user movement, and location accuracy. It also handles location update scheduling based on power consumption parameters you provide. In most cases, you'll get better battery performance, as well as more appropriate accuracy, by using the Location Services API.

Post a Comment for "Android Google Maps Api Location"