I have this code, that allows a user to select a location (mechanic_location) and passes it to another activity(mechanic_login). The problem is, it does not zoom. Help me enable it to zoom to the current location the user is in but give the user the ability to pick another location different from the one that he/she is in. Once the location is picked, click a button to send the latitude and longitude to the view (mechanic_login). Here is my code
public class mechanic_location extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
Button mSendLocationBtn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mechanic_location);
// 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;
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
#Override
public void onMapClick(LatLng latLng) {
Intent returnIntent = new Intent();
returnIntent.putExtra("picked_point",latLng);
setResult(Activity.RESULT_OK,returnIntent);
finish();
}
});
}
}
You can use the moveCamera() Method and defined the zoom level
float zoomLevel = 16.0f; //This goes up to 21
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, zoomLevel));
for mor information about using google map, you can check it here
source
Related
I have a google map in my app and I handle clicks on it. Everything is fine but if I click on a marker or really close to it onMarkerClick() is called, not onMapClick() and then I don't have the exact location of the place I tapped because marker.getPosition() returns the center point of the marker what's not the same.
Is it possible to disable onMarkerClick() and have onMapClick() called even if I click on a marker?
Return true for disabling marker click event
googleMap.setOnMarkerClickListener(new OnMarkerClickListener() {
#Override
public boolean onMarkerClick(Marker marker) {
return true;
}
});
For getting click on maps use this listener on onMapReady
#Override
public void onMapReady(GoogleMap googleMap) {
googleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener()
{
#Override
public void onMapClick(LatLng arg0)
{
Log.v("onMapClick", "Yea worked!");
}
});
}
Complete code
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// 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);
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
#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));
googleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
#Override
public void onMapClick(LatLng latLng) {
Toast.makeText(MapsActivity.this, "Map clicked", Toast.LENGTH_SHORT).show();
}
});
googleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(Marker marker) {
Toast.makeText(MapsActivity.this, "Marker disabled", Toast.LENGTH_SHORT).show();
return true;
}
});
}
}
It is possible.Here is a simple solution.
First of all you should declare GoogleMap.OnMarkerClickListener and GoogleMap.OnMapClickListener in your activity or fragment like this.
public class MapActivity extends FragmentActivity implements
GoogleMap.OnMarkerClickListener,
GoogleMap.OnMapClickListener
{
//..
}
And then register that interfaces in your onCreate() method.
#Override
protected void onCreate(Bundle savedInstanceState)
{
//..
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
mMap.setOnMarkerClickListener(this);
mMap.setOnMapClickListener(this);
}
Finally you can implement two interfaces.You should call onMapClick() method in onMarkerClick() method to achieve that even if you click on a marker like that.Don't forget to return true in onMarkerClick() method.
#Override
public void onMapClick(LatLng latLng) {
Log.v("onMapClick", "Map Clicked..")
}
#Override
public boolean onMarkerClick(Marker marker) {
onMapClick(marker.getPosition());
return true;
}
As of this morning I've tried to put together enough knowledge to produce a very basic app to demonstrate a concept. The idea is to display google maps, the user presses on where they want to add a marker, then a screen pops up where they can fill out more information that is then displayed when someone taps that marker.
This is what I got from the Android Studio base.
public class MapsActivity extends FragmentActivity implements
OnMapReadyCallback {
private GoogleMap mMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// 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);
}
private void setMapLongClick(final GoogleMap map) {
map.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {
#Override
public void onMapLongClick(LatLng latLng) {
}
});
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.z
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Move the camera to Delft
LatLng delft = new LatLng(52.003569, 4.372987);
Float zoom = 15f;
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(delft, zoom));
setMapLongClick(mMap);
}
}
I tried adding this
private void setMarkerClick(final GoogleMap map) {
map.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
Intent intent = new Intent(this, MainActivity.class);
this.startActivity(intent);
});
}
But I get and "illegal start of type" error. Am I doing this completely wrong?
Is there an easier way to add information to a marker?
This is how you should use it.
map.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(Marker marker) {
Intent intent = new Intent(MainActivity.this, MainActivity.class);
startActivity(intent);
return true;
}
});
Providing this as first argument when creating the Intent might not work since it refers to the OnMarkerClickListener anonymous class and not to the packageContext. Here's my suggestion, give MapsActivity.this as reference:
map.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(Marker marker) {
Intent intent = new Intent(MapsActivity.this, MainActivity.class);
startActivity(intent);
return true;
}
});
You cannot call
this.startActivity(intent);
inside an anonymous class as startActivity is not a method of the anonymous class but of the enclosing activity. Therefore you should use
startActivity(intent);
Also use the proper anonymous implementation.
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.
I am using Algolia's InstantSearch Android library.
This is my Query:
searcher.setQuery(new Query().setAroundLatLngViaIP(true).setAroundRadius(5000)).
But when I move the map, the markers stay in place. How can I display the new markers on the map when I move the map location view?
So basically you want to listen to the map changes and every time the user is dragging the map and reload the results? Here's what you should do:
public class AwesomeCameraActivity extends FragmentActivity implements
OnCameraIdleListener,
OnMapReadyCallback {
private GoogleMap mMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_camera);
SupportMapFragment mapFragment =
(SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap map) {
mMap = map;
mMap.setOnCameraIdleListener(this);
// Show the initial position
mMap.moveCamera(CameraUpdateFactory
.newLatLngZoom(new LatLng(YOUR_LATITUDE, YOUR_LONGITUDE), 10));
}
#Override
public void onCameraIdle() {
// The camera / map stopped moving, now you can fetch the center of the map
GeoPoint center = mMap.getProjection().fromPixels(mMap.getHeight() / 2, mMap.getWidth() / 2);
double centerLat = (double)center.getLatitudeE6() / (double)1E6;
double centerLng = (double)center.getLongitudeE6() / (double)1E6;
// Now fire a new search with the new latitude and longitude
searcher.setQuery(new Query().setAroundLatLng(new AbstractQuery.LatLng(centerLat, centerLng)).setAroundRadius(5000))
}
}
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