I am getting the place name of google maps using
Place place = PlaceAutocomplete.getPlace(MainActivity.this, data);
Logger.e("The ", "Place: " + place.getName());
now i want to set the marker at this place name. How can I acheive this?
First try to get the latitude and longitude from place name using Geocoder after that try following code for showing the marker on the map.
GoogleMap map;
map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
map.addMarker(new MarkerOptions()
.position(new LatLng(placelatitude, placeLongitude))
.title("Place Name"));
Related
I have a google map with some markers and all of them have title and snippet. I store markers in a HashMap. I want to display title and snippet with showInfoWindow() function when a button clicked.
this is my code :
Marker m = getMarker(location); //this function return marker from HashMap with its location
if(m!=null)
m.showInfoWindow();
m is not null, but showInfoWindow() doesn't work
debug output:
m.getTitle() log :
Change your code to this
Marker m = getMarker(location);
if(m!= null){
m.showInfoWindow();
}
The Marker object will be null if the marker has not been rendered on the map yet so be sure to check that the marker object is not null before using it.
For showing infoWindow your marker must be have a title.
MarkerOptions marker = new MarkerOptions().position(location)
.icon(BitmapDescriptorFactory.fromBitmap(aBitmapFile))
.title("The Title"); //here is title
mapPoint p = new mapPoint(lat, lng);
googleMap.addMarker(marker);
then you can call: .showInfoWindow(); on your marker.
I have multiple locations that I need to display on my map.
I want the info/label for each marker to display on the maps along with the pin icon whether the marker/pin is clicked or not by the user.
I have the following code to display the markers.
for (DepartmentLocation loc : departmentLocations) {
if (loc != null && loc.getLatitude() != null && loc.getLongitude() != null) {
LatLng latLng = new LatLng(loc.getLatitude(), loc.getLongitude());
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
googleMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
MarkerOptions markerOptions = new MarkerOptions().position(latLng).title(loc.getName());
markerOptions.visible(true);
Marker marker = googleMap.addMarker(markerOptions);
marker.showInfoWindow();
marker.setVisible(true);
markers.add(marker);
}
}
But still, I see only the red pins and no labels beside the pin.
Here is how my maps look like. I can see only one info label, not the others.
I have tried using the IconGenerator from the map utils.
implementation 'com.google.maps.android:android-maps-utils:0.5'
for (DepartmentLocation loc : departmentLocations) {
if (loc != null && loc.getLatitude() != null && loc.getLongitude() != null) {
LatLng latLng = new LatLng(loc.getLatitude(), loc.getLongitude());
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
googleMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
MarkerOptions markerOptions = new MarkerOptions()
.icon(BitmapDescriptorFactory.fromBitmap(iconFactory.makeIcon(loc.getName())))
.position(latLng)
.title(loc.getName());
markerOptions.visible(true);
Marker marker = googleMap.addMarker(markerOptions);
marker.showInfoWindow();
marker.setVisible(true);
markers.add(marker);
}
}
When I use iconfactory, I am unable to show both the pin and the info label.
Only one InfoWindow is displayed at a time on GoogleMap. You can't display multiple InfoWindow at a time.
You have to use IconGenerator for that. You can set a view to IconGenerator and make a Bitmap form that view. You can make a view that contains both Marker and your InfoWindow View. Use that view to crate your Desired Marker.
You can check this answer
From the documentation, there is a text saying that the info window is shown one at a time, so there is no way to do that:
"An info window allows you to display information to the user when they
tap on a marker. Only one info window is displayed at a time. If a
user clicks on a marker, the current info window will be closed and
the new info window will be displayed. Note that if the user clicks on
a marker that is currently showing an info window, that info window
closes and re-opens."
You can read more here: https://developers.google.com/maps/documentation/android-sdk/infowindows
I want to make google maps api to provide result of search by category as the nearest hotels available in certain range and i want also to control range , here is the code i started with , this code search when the use apply an address . i want this this to done when the user enter a certain category not by entering an address
protected void search(List<Address> addresses) {
Address address = (Address) addresses.get(0);
home_long = address.getLongitude();
home_lat = address.getLatitude();
latLng = new LatLng(address.getLatitude(), address.getLongitude());
addressText = String.format(
"%s, %s",
address.getMaxAddressLineIndex() > 0 ? address
.getAddressLine(0) : "", address.getCountryName());
markerOptions = new MarkerOptions();
markerOptions.position(latLng);
markerOptions.title(addressText);
map1.clear();
map1.addMarker(markerOptions);
map1.moveCamera(CameraUpdateFactory.newLatLng(latLng));
map1.animateCamera(CameraUpdateFactory.zoomTo(15));
locationTv.setText("Latitude:" + address.getLatitude() + ", Longitude:"
+ address.getLongitude());
}
what the modification needed to be done in this code ?
You have two options here:
1st option- You build your own DB with all the hotels and their locations, then reflect that on the map.
2nd option- you use Google's Places API. Here is a nice discussion about it.
Hope this helps :)
below is my code which give hardcoded lat long of user i want to show dynamic latlong where ever user go map show current location of user how to do this
?
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_support_map_fragment);
FragmentManager fmanager = getSupportFragmentManager();
Fragment fragment = fmanager.findFragmentById(R.id.map);
SupportMapFragment supportmapfragment = (SupportMapFragment)fragment;
GoogleMap supportMap = supportmapfragment.getMap();
LatLng latLng = new LatLng(24.89337, 67.02806);
supportMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
supportMap.addMarker(new MarkerOptions()
.position(latLng)
.title("My Spot")
.snippet("This is my new spot!")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
supportMap.getUiSettings().setCompassEnabled(true);
supportMap.getUiSettings().setZoomControlsEnabled(true);
supportMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 20));
}
Try adding this line:
supportMap.setMyLocationEnabled(true);
You obviously need to get the current location of the user. You need to use LocationListener to request continuous updates. You can specify the time interval or the distance interval between the location updates. This with give you a Location object from which you can extract the latitude and longitude. You can then use these 2 values to point the current location on the map. There are numerous questions and great answers on How to get the current location. You can start by reading:
Location Strategies Google's documentation
Some of the previous questions are:
How to get Latitude and Longitude of the mobile device in android?
how to get longitude and latitude in android
find current location latitude and longitude
How to get the current location latitude and longitude in android
Hope this helps.
Here i am able to display User on google map by getting user latitude and longitude now i want to display near by events happening in user location and want to display nearby venues so chosen foursquare api.here it is
2nd one
Here is my code to display user on map
R.id.map)).getMap();
// Creating location manager and location classes instances
locationManager = (LocationManager) this
.getSystemService(LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
0, this);
;
mCurrentLocation = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
// if location found display as a toast the current latitude and
// longitude
if (mCurrentLocation != null) {
Toast.makeText(
this,
"Current location:\nLatitude: "
+ mCurrentLocation.getLatitude() + "\n"
+ "Longitude: " + mCurrentLocation.getLongitude(),
Toast.LENGTH_LONG).show();
USER_LOCATION = new LatLng(mCurrentLocation.getLatitude(), mCurrentLocation.getLongitude());
// adding marker
googleMap.addMarker(new MarkerOptions().position(USER_LOCATION).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ROSE)).title("Your current location"));
// Move the camera instantly to hamburg with a zoom of 15.
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(USER_LOCATION, 12));
// Zoom in, animating the camera.
googleMap.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
give me a brief idea how to use that api.based on my code.
Sorry, but do you want a complete tutorial for copy&paste?
You managed to get a location, you have a documentation of the api you want to use... so use it?
Take the location you have from Android. Format it so that the api can use it (perhaps they need coordinates, perhaps a city name, ...) and then call the web service. You will get a result and have to interpret it.
Where is actually your problem?