I added map in android application and want to add marker on map by address. It is possible?
I already tried to get long and lat with Geocoder
, but there I get error Service not Available
.
my code:
Geocoder geocoder = new Geocoder(getBaseContext());
List<Address> addresses = null;
try {
addresses = geocoder.getFromLocationName(event.getPlace(), 20);
System.out.println(addresses);
// for (int i = 0; i < addresses.size(); i++) { // MULTIPLE MATCHES
//
// Address addr = addresses.get(i);
//
// double latitude = addr.getLatitude();
// double longitude = addr.getLongitude(); // DO SOMETHING WITH
// // VALUES
//
// System.out.println(latitude);
// System.out.println(longitude);
//
// }
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
For adding a custom marker to Google Maps navigate to the app > res > drawable > Right-Click on it > New > Vector Assets and select the icon which we have to show on your Map. You can change the color according to our requirements. After creating this icon now we will move towards adding this marker to our Map.
Share a map or location Or, find a place on the map, then touch and hold to drop a pin. At the bottom, tap the place name or address. Share. Choose the app where you want to share the link to the map.
Create a method to convert address to LatLng:
public LatLng getLocationFromAddress(Context context, String strAddress)
{
Geocoder coder= new Geocoder(context);
List<Address> address;
LatLng p1 = null;
try
{
address = coder.getFromLocationName(strAddress, 5);
if(address==null)
{
return null;
}
Address location = address.get(0);
location.getLatitude();
location.getLongitude();
p1 = new LatLng(location.getLatitude(), location.getLongitude());
}
catch (Exception e)
{
e.printStackTrace();
}
return p1;
}
then ,
@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 address = getLocationFromAddress(this, yourAddressString(eg. "Street Number, Street, Suburb, State, Postcode");
mMap.addMarker(new MarkerOptions().position(address).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(address));
}
It is possible. Your problem seems to be the geocoding part.
The problem with your use of Geocoder is that it requires a backend service that is not included in the core android framework, as mentioned in the Android API for Geocoder. You should use geocoder.isPresent()
to check if this functionality is available. If it is not, you cannot use this method.
Geocoding can alternatively be done with Google Maps using a URL, as described in The Google Geocoding API. For example (you need an API key):
https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&key=API_KEY
Giving a result that can be parsed to retrieve latitude and longitude for your marker.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With