Google Maps Pin in my Android application - java

I need to display user inserted pins on the map that i will have in my application.
This pin needs to be visible to all the users using the app.
I have no clue how to implement this.
Can i get some help please?

Pins in Google Maps are called Markers and they are pretty simple to create.
Following what is documented from the Google Maps API here, you just use the addMarker() method to create a marker and can customize the options of where it should be placed and the title to show when it is clicked on
#Override
public void onMapReady(GoogleMap map) {
map.addMarker(new MarkerOptions()
.position(new LatLng(10, 10))
.title("Hello world"));
}

Related

Add marker using Lat/Lng when Google Map loads

I want to know
if we can add marker in Google Maps using longitude and latitude provided from firebase database. When maps fragment is loaded.
In simple I want to add markers in Google Maps using my own longitude and latitude and it should mark all the places say if I have 10 place's lng and lat.
Now I want to show all 10 places on Google Maps and some additional information also
Yes you can. Ther is a function addMarker() on map object to do that:
#Override
public void onMapReady(GoogleMap map) {
map.addMarker(new MarkerOptions()
.position(new LatLng(10, 10))
.title("Hello world"));
}
Documentation:
https://developers.google.com/maps/documentation/android-api/marker

Get current location when button is clicked

I have a google maps app and now I've added a button. I want to get a message (Toast) with my coordinates when I click the button. Which is the simplest way to do this? Do I also have to implement onLocationChanged class?
You can use different ways to implement it. I tend to think that the best for your task is to show toast after pressing my location button.
public class MapActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
/**
* 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;
//Show my location button
mMap.setMyLocationEnabled(true);
mMap.setOnMyLocationButtonClickListener(new GoogleMap.OnMyLocationButtonClickListener() {
#Override
public boolean onMyLocationButtonClick() {
//Do something with your location. You can use mMap.getMyLocation();
//anywhere in this class to get user location
Toast.makeText(MapActivity.this, String.format("%f : %f",
mMap.getMyLocation().getLatitude(), mMap.getMyLocation().getLongitude()),
Toast.LENGTH_SHORT).show();
return false;
}
});
}
}
Also you can show toast with user location every time it changes. To do it set OnMyLocationChangeListener
mMap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() {
#Override
public void onMyLocationChange(android.location.Location location) {
//...
}
});
Do I also have to implement onLocationChanged class?
If you don't want to receive location updates, the answer is no.
Which is the simplest way to do this?
The simplest way is to obtain the last known location as described here.
The Example Google maps project on github has an example of how to do this.
Additionally, Android Studio has a Google Maps Activity template you can choose when you create a project. This project has getLastKnownLocation() implemented. More information can be found here.

Clicking on marker to edit marker info in android

I am making an app that uses google maps and markers, and would like to enable users to long click on map to add a marker, and get a popup that would allow them to name the marker, write a short summary about it, etc. I would also like to enable the users to click on the existing marker and to get a popup with the info they entered previously and that they could edit at any time. At first I tried doing something like this:
mMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {
#Override
public void onMapLongClick(LatLng arg0) {
startActivity(new Intent(MapsActivity.this, SetMarkerInfoActivity.class));
}
});
I would open new activity when i long click on the map. I added marker code to the SetMarkerInfoActivity:
marker = mMap.addMarker(new MarkerOptions()
.icon(BitmapDescriptorFactory.fromResource(R.drawable.custom_marker))
.position(
new LatLng(arg0.latitude,
arg0.longitude))
.visible(true));
But this seemed like bad solution. Does anyone have some better idea?

How to show a label(String) over a marker in google maps static api?

I am working on a vehicle routing algorithm I need to show vehicles on maps with there driver name on top of the label. I have done the marker part it looks like this.
However I would like to display driver's name on top of every marker. Can you suggest me any way to do it?
My google maps static api url looks like this
https://maps.googleapis.com/maps/api/staticmap?center=26.900,75.800&zoom=11&size=400x400&markers=color:blue|label:Driver|&markers=color:green|label:Req|26.925571778374632,75.81166033197894|&markers=color:blue|label:others|26.944421737574313,75.8072312248272|26.941722318252367,75.82851178160593|26.909618922760885,75.85079885210592|26.92537722447672,75.85626510000787|26.969253598622025,75.81687986464266|26.933707124740607,75.84999920863306|26.986333832648626,75.88755301718626|26.945330532812793,75.81567681826434|26.942938812152843,75.81989078933901|&markers=color:red|label:Req|26.8983489,75.796436|
Ps: I am using java.
I think the best you can do with the Static Maps API v2 is to have different initials and colors for different markers. Like this example.
You may be able to do something with the custom icons too, but it does not seem to be working as far as I can tell.
The best practice here is to implement it using the JavaScript API v3 instead.
Is this possible with the version you're using?
#Override
public void onMapReady(GoogleMap map) {
LatLng sydney = new LatLng(-33.867, 151.206);
map.setMyLocationEnabled(true);
map.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 13));
map.addMarker(new MarkerOptions()
.title("Sydney")
.snippet("The most populous city in Australia.")
.position(sydney));
}
Here is the code in action: https://developers.google.com/maps/documentation/android/

Google maps marker show only icon

I want to display markers on google maps (android)
I used this code (like in the example in android developer):
The location, name and descripion are valid (I checked them via debug mode)
It shows me the icon on the desired location, however without the marker or the description and snippets. The following line
mMap.addMarker(MarkerOptions().position(location)
.title(poi.getName()).snippet(poi.getDescription())
.icon(BitmapDescriptorFactory.fromBitmap(image_item));
shows me only the icon without the title or snippest.
Thanks a lot
Did you follow the examples from the documentation? Also, the sample code bundled with the Google Play services SDK?
Edit: You can show an info window programmatically by calling showInfoWindow() on the target marker:
Marker marker = mMap.addMarker(new MarkerOptions()
.position(location)
.title(poi.getName())
.snippet(poi.getDescription())
.icon(BitmapDescriptorFactory.fromBitmap(image_item));
marker.showInfoWindow();
However, bear in mind that only one info window can be displayed at a time. If a user clicks on another marker, the current window will be hidden and the new info window will be displayed.
try this code.
This code is work on Google map with API V2
Add the inbuilt method of google.
public void onMapLongClick(LatLng point) {
// Getting the Latitude and Longitude of the touched location
latLng = point;
// Clears the previously touched position
myMap.clear();
// Animating to the touched position
myMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
// Creating a marker
markerOptions = new MarkerOptions();
// Setting the position for the marker
markerOptions.position(latLng);
// Adding Marker on the touched location with address
new ReverseGeocodingTask(getBaseContext()).execute(latLng);
tmpLatLng = latLng;
drawCircle(point);
btLocInfo.setEnabled(true);
}
This is not a rare problem the only way to fix it is to reinstall its the only way was able to fix it .
show icon without the title or snippest. use below code
LatLng newYork = new LatLng(40.7142, 74.0064);
Marker melbourne = map.addMarker(new MarkerOptions()
.position(newYork));
Edited:
show marker with title & Snippest & icon
map.addMarker(new MarkerOptions().position(new LatLng(lat, lon))
.title(string).snippet(string2).icon(BitmapDescriptorFactory.fromBitmap(image_item));

Categories