I extend my app with google map activity. it created a mapclass and extends FragmentActivity implements OnMapReadyCallback.
the onMapReadyCallBack return GoogleMap object as below code.
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
public GoogleMap mMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user);
// 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);
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}
I want to access mMap on my MainActivity class to do some operation. i create a button with onclick attribute to gotolocation method. but it return error becuase the map object is null; which is the proper way to access the GoogleMap object in my activity class.
public void gotoLocation(View v){
if (mMap != null){
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Kabul"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}else{
Log.d("Map object", "It si null==============");
}
}
Keep in mind that only one Activity at a time can be shown to the user, so there is no need to access the mMap variable from MainActivity.
Assuming you want to open up MapsActivity when the gotoLocation() button is clicked, you can pass the data associated with the location from MainActivity over to MapsActivity.
First, prepare the LatLng and description in MainActivity in order to send it to MapsActivity when the button is clicked:
public void gotoLocation(View v){
LatLng goToLocation = new LatLng(34.5392354, 69.1378334);
Bundle args = new Bundle();
args.putParcelable("latLon", goToLocation);
args.put("desc", "Marker in Kabul");
Intent i = new Intent(this, MapsActivity.class);
i.putExtras(args);
startActivity(i);
}
Then, when MapsActivity is opened, it will retrieve the info, and modify the Marker accordingly:
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
public GoogleMap mMap;
LatLng mLatLng;
String mDescription;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user);
//Get the data sent from MainActivity
Intent intent = getIntent();
mLatLng = intent.getParcelableExtra("latLon");
mDescription = intent.getStringExtra("desc");
// 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);
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
//Use the data sent from MainActivity:
if (mLatLng != null) {
// Add a marker for location/description sent from MainActivity
mMap.addMarker(new MarkerOptions().position(mLatLng).title(mDescription));
mMap.moveCamera(CameraUpdateFactory.newLatLng(mLatLng));
}
}
}
In addition to first answer, the mMap object is null because it is yet to be instantiated and onMapReady callback has not yet been called.
take a look at the docs for OnMapReadyCallback interface https://developers.google.com/android/reference/com/google/android/gms/maps/OnMapReadyCallback
Related
I pass data of longitude and latitude from one activity to another activity, and it is successful, but how can I after received this data Display it on Google Maps as a location so the user will be see location in google map not see the number of longitude and latitude.
Note I pass data by use getExtra() and putExtra()
If anyone know solution please help me
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(Main3Activity.this, MapsActivity.class);
intent.putExtra("Latitude", Latitude);
intent.putExtra("Longitude", Longitude);
startActivity(intent);
}
});
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
Intent i = getIntent();
final String Latitude=i.getStringExtra("Latitude");
final String Longitude=i.getStringExtra("Longitude");
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}
Change
final Double Latitude=i.getStringExtra("Latitude");
final Double Longitude=i.getStringExtra("Longitude");
to
final String Latitude=i.getDoubleExtra("Latitude");
final String Longitude=i.getDoubleExtra("Longitude");
Change LatLng sydney = new LatLng(-34, 151); to LatLng sydney = new LatLng(Latitude, Longitude);
Try this code:
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
Double Latitude=0.0;
Double Longitude=0.0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
Intent i = getIntent();
Latitude=i.getDoubleExtra("Latitude",0.0);
Longitude=i.getDoubleExtra("Longitude",0.0);
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
LatLng sydney = new LatLng(Latitude, Longitude);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}
I want to register a OnMarkerClickListener to a GoogleMap:
implement the GoogleMap.OnMarkerClickListener interface
register listener via GoogleMap.setOnMarkerClickListener(this)
but when I click on a marker the callback onMarkerClick() is never called. For test purposes I also implemented OnMapLongClickListener and it works perfectly well.
public class ShowCoords extends FragmentActivity implements
OnMapReadyCallback,
LocationListener,
GoogleMap.OnCameraMoveListener,
GoogleMap.OnCameraMoveStartedListener,
GoogleMap.OnMyLocationButtonClickListener,
GoogleMap.OnMyLocationClickListener,
GoogleMap.OnMarkerClickListener,
GoogleMap.OnMapLongClickListener {
private GoogleMap mMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.show_coords);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.getUiSettings().setZoomControlsEnabled(true);
mMap.getUiSettings().setScrollGesturesEnabled(true);
mMap.getUiSettings().setCompassEnabled(true);
mMap.getUiSettings().setMapToolbarEnabled(false);
if (gpsPermission()) {
mMap.setMyLocationEnabled(true);
}
mMap.setOnMyLocationButtonClickListener(this);
mMap.setOnMyLocationClickListener(this);
mMap.setOnCameraMoveStartedListener(this);
mMap.setOnCameraMoveListener(this);
mMap.setOnMarkerClickListener(this);
mMap.setOnMapLongClickListener(this);
if (displayFormat.equals(getString(R.string.heatmapValue))) {
// display markers as heatmap
} else if (displayFormat.equals(getString(R.string.clusterValue))) {
// diplay markers as cluster
mClusterManager = new ClusterManager<>(this, mMap);
mMap.setOnCameraIdleListener(mClusterManager);
// this causes the problem
mMap.setOnMarkerClickListener(mClusterManager);
for (LatLng p : coords) {
mClusterManager.addItem(new MyClusterItem(p));
}
} else {
Log.w(TAG, "onMapReady: coords still null, nothing to display");
}
//just for testing
mMap.addMarker(new MarkerOptions().position(new LatLng(51.866404, 12.643411)));
}
#Override
public boolean onMarkerClick(Marker marker) {
Log.i(TAG, "onMarkerClick");
return true;
}
#Override
public void onMapLongClick(LatLng latLng) {
Log.i(TAG, "onMapLongClick");
}
// removed other callbacks
}
Any ideas, whats going wrong?
I got it. When I display the markers as a cluster (via Google Maps Android Marker Clustering Utility) the clustermanager registers as OnMarkerClickListener and the registration of my listener get lost. As a workaround, I omit the clustermanager registration and pass the click event on the clustermanager by myself.
My map is working fine.However, i want to add a satellite view along with my normal view? How can i achieve that?
public class MainActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
#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) {
mMap = googleMap;
LatLng location = new LatLng(x,y);
mMap.addMarker(new MarkerOptions().position(ReduitBusStop).title("you are here!"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(location));
Try with setting the type of map tiles as below
mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
Use the below function to set the satellite view
setMapType(com.google.android.gms.maps.GoogleMap.MAP_TYPE_SATELLITE);
I want to replace the deprecated getMap Method with getMapAsync, but I didn't use MapFragment but GoogleMap like this:
private GoogleMap googleMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
try {
if(googleMap == null) {
googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
}
googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
googleMap.setMyLocationEnabled(true);
googleMap.setTrafficEnabled(true);
googleMap.setIndoorEnabled(true);
googleMap.setBuildingsEnabled(true);
googleMap.getUiSettings().setZoomControlsEnabled(true);
If I replace the googleMap with MapFragment like this I'm not able anymore to setMapType and so on. So how can I change to getMapAsync in my case?
MapFragment mapFragment = (MapFragment) getFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
as in the official doc, get map async requires a callback;
it's there your "main entry point" for google maps stuff!
public class MapPane extends Activity implements OnMapReadyCallback {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map_activity);
MapFragment mapFragment = (MapFragment) getFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap map) {
// DO WHATEVER YOU WANT WITH GOOGLEMAP
map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
map.setMyLocationEnabled(true);
map.setTrafficEnabled(true);
map.setIndoorEnabled(true);
map.setBuildingsEnabled(true);
map.getUiSettings().setZoomControlsEnabled(true);
}
}
Very simple, just have your Activity implement the OnMapReadyCallback interface, and then assign your googleMap reference in the onMapReady() callback.
Then, perform any actions on googleMap that you want.
Here is a simple example:
public class MainActivity extends Activity implements OnMapReadyCallback {
private GoogleMap googleMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MapFragment mapFragment = (MapFragment) getFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap map) {
googleMap = map;
setUpMap();
}
public void setUpMap(){
googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
googleMap.setMyLocationEnabled(true);
googleMap.setTrafficEnabled(true);
googleMap.setIndoorEnabled(true);
googleMap.setBuildingsEnabled(true);
googleMap.getUiSettings().setZoomControlsEnabled(true);
}
}
If you use MapFragment you can still use stMapType method. Implement OnMapReadyCallback on your activity.
Create MapFragment and call getAsyncMap method on that object.
It will ask you to inplement onMapReady(GoogleMap map) method there you can set map type and so on.
Hope it helps.
The better way I found to understand the use of getMapAsync was to create a new project in Android Studio and use the template of google maps activity instead of an empty activity.
You will have a project that works to study and adapt your owns.
I had a problem on how to enter the latitude and longitude of my database SQLiteDatabaseAssets, this code I get from google sites and i don't know how the code to changes the LatLng(-7.963535, 112.640756) with String which ordered by name of the lat and long
public class MainActivity extends Activity implements OnMapReadyCallback {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
MapFragment mapFragment = (MapFragment) getFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap map) {
LatLng malang = new LatLng(-7.963535, 112.640756);
==>> // LatLng malang = new LatLng("it must be get from my database ordered by name");
map.setMyLocationEnabled(true);
map.moveCamera(CameraUpdateFactory.newLatLngZoom(malang, 13));
map.addMarker(new MarkerOptions()
.title("it must be get from my database")
.position(malang));
map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
map.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
map.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
}}
can anyone help me please? i was tired to find the code :-(
i was already prepared the database in /assets/mydatabase