See if the example helps, I just do not know how to make the emulator work.
** In AndroidManifest.XML add
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
** Gradle script (Module app) dependences add
compile 'com.google.android.gms:play-services-maps:9.4.0'
compile 'com.google.android.gms:play-services-location:9.4.0'
**
//-------------------------------------
public class MainActivity extends AppCompatActivity implements OnMapReadyCallback
{
protected GoogleMap m_map;
//-----------------------------------
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
//-----------------------------------
@Override
public void onMapReady(GoogleMap googleMap)
{
m_map = googleMap;
m_map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
UiSettings ui = m_map.getUiSettings();
ui.setZoomControlsEnabled(true);
try {
checkCnn();
checkGps();
new MyLocation().execute();
}
catch (Exception e) {
// Tratar error
Toast.makeText(this,e.getMessage(),Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
//-----------------------------------
public void checkCnn() throws Exception
{
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
if (activeNetwork == null) {
throw new Exception("wi-fi off");
}
}
//-----------------------------------
public void checkGps() throws Exception
{
LocationManager manager;
manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
throw new Exception("gps off");
}
}
//-----------------------------------
protected class MyLocation implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener
{
private GoogleApiClient m_api;
//---------------------------------
public MyLocation()
{
super();
}
//---------------------------------
protected void execute()
{
m_api = new GoogleApiClient.Builder(MainActivity.this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
m_api.connect();
}
//-----------------------------------
@Override
public void onConnected(Bundle bundle)
{
LatLng latLng = location();
m_map.addMarker(new MarkerOptions().position(latLng));
m_map.animateCamera(cameraPosition(latLng,15,0,0));
//m_map.moveCamera(cameraPosition(latLng,15,0,0));
m_api.disconnect();
}
//---------------------------------
@Override
public void onConnectionSuspended(int i)
{
}
//---------------------------------
@Override
public void onConnectionFailed(ConnectionResult connectionResult)
{
}
//---------------------------------
public CameraUpdate cameraPosition(LatLng latLng, float zoom, float tilt, float bearing)
{
CameraPosition.Builder builder = new CameraPosition.Builder();
CameraPosition position = builder.target(latLng)
.zoom(zoom)
.tilt(tilt)
.bearing(bearing)
.build();
return CameraUpdateFactory.newCameraPosition(position);
}
//---------------------------------
public LatLng location()
{
Location loc = LocationServices.FusedLocationApi.getLastLocation(m_api);
return new LatLng(loc.getLatitude(),loc.getLongitude());
}
//-----------------------------------
} // end MyLocation
//-----------------------------------
} // end class
//-------------------------------------