Skip to content

Instantly share code, notes, and snippets.

@prasad091
Created March 20, 2017 10:23
Show Gist options
  • Save prasad091/37ac7fbf0fe220814c28d92892d6f293 to your computer and use it in GitHub Desktop.
Save prasad091/37ac7fbf0fe220814c28d92892d6f293 to your computer and use it in GitHub Desktop.
//copy your map_activity.xml in layout folder
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
public class MapActivity extends AppCompatActivity implements OnMapReadyCallback {
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map_activity);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
}
/**
*
* @param googleMap
* onMapReady method Invoke the current lat and lang in onMap Ready
*/
@Override
public void onMapReady(GoogleMap googleMap) throws Resources.NotFoundException{
mMap = googleMap;
mMap.setMapStyle(MapStyleOptions.loadRawResourceStyle(this, R.raw.mapstyle_aubergine));
LatLng hcmus = new LatLng(11.0791, 77.0017);
CameraPosition cameraPosition =
new CameraPosition.Builder()
.target(hcmus)
.bearing(45)
.tilt(90)
.zoom(googleMap.getCameraPosition().zoom)
.build();
CameraUpdate cameraUpdate = CameraUpdateFactory.newCameraPosition(cameraPosition);
mMap.animateCamera(
cameraUpdate,
300,
new GoogleMap.CancelableCallback() {
@Override
public void onFinish() {
}
@Override
public void onCancel() {
}
}
);
// mMap.moveCamera(cameraUpdate);
originMarkers.add(mMap.addMarker(new MarkerOptions()
.title("Alumni")
.position(hcmus)));
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
mMap.setMyLocationEnabled(true);
}
// copy your gradle dependency
compile 'com.google.android.gms:play-services:9.6.1'
// inside your manifest application
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment