I use below code to search on google map android
// An AsyncTask class for accessing the GeoCoding Web Service
private class GeocoderTask extends AsyncTask<String, Void, List<Address>>{
#Override
protected List<Address> doInBackground(String... locationName) {
// Creating an instance of Geocoder class
Log.d("bagibagi","doInBackground");
Geocoder geocoder = new Geocoder(getBaseContext());
List<Address> addresses = null;
try {
addresses = geocoder.getFromLocationName(locationName[0], 1);
} catch (IOException e) {
e.printStackTrace();
}
return addresses;
}
#Override
protected void onPostExecute(List<Address> addresses) {
Log.d("bagibagi","onPostExecute");
search = "";
if(addresses==null || addresses.size()==0){
Toast.makeText(getBaseContext(), "No Location found OR you use this several time", Toast.LENGTH_SHORT).show();
}
else
{
// Adding Markers on Google Map for each matching address
for(int i=0;i<addresses.size();i++){
Address address = (Address) addresses.get(i);
// Creating an instance of GeoPoint, to display in Google Map
latLng = new LatLng(address.getLatitude(), address.getLongitude());
String addressText = String.format("%s, %s",
address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : "",
address.getCountryName());
markerOptions = new MarkerOptions();
//markerOptions.icon(icon);
markerOptions.position(latLng);
markerOptions.title(addressText);
//map.addMarker(markerOptions);
Toast.makeText(getApplicationContext(), addressText, 1).show();
// Locate the first location
if(i==0){
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(latLng) // Sets the center of the map to Mountain View
.zoom(13)
// Sets the zoom
//.bearing(90) // Sets the orientation of the camera to east
//.tilt(30) // Sets the tilt of the camera to 30 degrees
.build(); // Creates a CameraPosition from the builder
map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
}
}
}
}
how i can change .zoom(13) for a city search or a Country name search.
for example when user search a country map must zoom smaller than search a city.
below image show you thing that i want.
You can use Google Places API to obtain the Viewport of particular place.
Make request to this url: http://maps.googleapis.com/maps/api/geocode/json?address=YOUR-COUNTRY-OR-CITY&sensor=true
In the JSON response you will find the following keys:
geometry:{
bounds:{
northeast:{
lat:40.501368,
lng:-79.8657231
},
southwest:{
lat:40.3613689,
lng:-80.0952779
}
},
location:{
lat:40.44062479999999,
lng:-79.9958864
},
location_type:"APPROXIMATE",
viewport:{
northeast:{
lat:40.501368,
lng:-79.8657231
},
southwest:{
lat:40.3613689,
lng:-80.0952779
}
}
}
Either go for "geometry" key data or "viewport"
Create new LatLngBounds (LatLng southwestParsedCoordinate, LatLng northeastParsedCoordinate) object and move the camera to the that bound object
mMap.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 10);
Related
I am working on google map app.when i define a route For example from city one to city two ..then how can i get the latitude and longitude of all places coming between city one and city two.
destinationid= (EditText) findViewById(R.id.destinationid);
String clocation = destinationid.getText().toString();
List<Address> addressList=null;
if(clocation!= null || clocation!="")
{
Geocoder geocoder=new Geocoder(this);
try {
Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("google.navigation:q="+clocation+""));
startActivity(intent);
addressList= geocoder.getFromLocationName(clocation,1);
}
catch (IOException e)
{
e.printStackTrace();
}
Address address= addressList.get(0);
LatLng latLng=new LatLng(address.getLatitude(),address.getLongitude());
mMap.addMarker(new MarkerOptions().position(latLng).title("Current Location "));
mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.addMarker(new MarkerOptions().position(latLng).title("Current Location "));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 15.0f));
mMap.animateCamera(CameraUpdateFactory.zoomIn()); mMap.animateCamera(CameraUpdateFactory.zoomTo(15), 2000, null); CameraPosition cameraPosition = new CameraPosition.Builder()
.target(latLng)
.zoom(15)
.bearing(150)
.tilt(70)
.build();
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
mMap.setTrafficEnabled(true);
}'
To answer your first question in comment above,
1] to show any particular point B on your route you can use markers, this is a feature of google maps API, more details about this you can find here.
2] to check anyone is in your radius you can use Geo fence API. you can find more details here.
I'm making an app that when it opens ,it shows the user's current location.The thing is that I want also to put all the details(home address,postal code,country) by pressing on the marker just like this application
Application Photo
Code:
private GoogleMap mMap;
private GoogleApiClient googleApiClient;
private LocationRequest locationRequest;
private Location lastLocation;
private Marker currentUserLocationMarker;
here is the method that contains the marker
#Override
public void onLocationChanged(Location location) {
lastLocation=location;
if(currentUserLocationMarker!=null)
{
currentUserLocationMarker.remove();
}
LatLng latLng= new LatLng(location.getLatitude(),location.getLongitude());
MarkerOptions markerOptions= new MarkerOptions();
markerOptions.position(latLng);
markerOptions.title("Test");
markerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.map_marker_mini));
currentUserLocationMarker = mMap.addMarker(markerOptions);
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.animateCamera(CameraUpdateFactory.zoomBy(15));
if(googleApiClient != null)
{
LocationServices.FusedLocationApi.removeLocationUpdates(googleApiClient,this);
}
}
You can use the Geocoding API and use reverse geocoding to get the details from the latlng. Here is an example reverse geocoding query,
https://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&key=YOUR_API_KEY
This will return a json response with multiple address results with first address as the most prominent result.
You can also use Geocoder in android as follows, (This example gets the list of addresses and logs the formatted adress line from the returned results)
Geocoder geocoder=new Geocoder(this);
try {
List<Address> addressList=geocoder.getFromLocation(40.714232,-73.9612889,10);
for(Address address:addressList){
Log.d("TAG",address.getAddressLine(0));
}
} catch (IOException e) {
e.printStackTrace();
}
The parms of getFromLocation(..) are lat,lng and maximum number of results you want to retrieve with first againg being the prominent one.
Checkout the documentation of Geocoding API to know what else you can do,
https://developers.google.com/maps/documentation/geocoding/intro#reverse-example
Also checkout the documentation of Geocoder,
https://developer.android.com/reference/android/location/Geocoder#Geocoder(android.content.Context,%20java.util.Locale)
Hi I want place markers on all shoppers stop stores which are near to user current location. I am using following code
currently I am getting only 2 locations, I want set some radius and want to display all the stores also.
private class GeocoderTask extends AsyncTask<String, Void, List<Address>> {
#Override
protected List<Address> doInBackground(String... locationName) {
// Creating an instance of Geocoder class
Geocoder geocoder = new Geocoder(getBaseContext());
List<Address> addresses = null;
try {
// Getting a maximum of 3 Address that matches the input text
addresses = geocoder.getFromLocationName(locationName[0], 10);
} catch (IOException e) {
e.printStackTrace();
}
return addresses;
}
#Override
protected void onPostExecute(List<Address> addresses) {
if(addresses==null || addresses.size()==0){
Toast.makeText(getBaseContext(), "No Location found", Toast.LENGTH_SHORT).show();
}
// Clears all the existing markers on the map
googleMap.clear();
// Adding Markers on Google Map for each matching address
for(int i=0;i<addresses.size();i++){
Address address = (Address) addresses.get(i);
// Creating an instance of GeoPoint, to display in Google Map
latLng = new LatLng(address.getLatitude(), address.getLongitude());
String addressText = String.format("%s, %s",
address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : "",
address.getCountryName());
markerOptions = new MarkerOptions();
markerOptions.position(latLng);
markerOptions.title(addressText);
googleMap.addMarker(markerOptions);
// Locate the first location
if(i==0)
googleMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
}
}
}
but unable to get relevant results. Help me.
You should have a look at nearby search of Places API web service.
To make your life easier you can also use the Java Client for Google Maps Services located at github:
https://github.com/googlemaps/google-maps-services-java
Follow documentation of the Java Client library and you should be able to search nearby places for given location and radius.
I hope this helps!
I'm making an app with a Google Maps Activity and I want to place a marker there. How do I set a marker to a location, for example the White House, so that it opens up the info page in google maps when you press the maps button in the right corner? My code:
LatLng latLng = new LatLng(38.897677, -77.036531);
mMap.addMarker(new MarkerOptions().position(latLng).title("The White House"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
Here is answer of your question what I am perceived that actually you wants to implement a click Listener.
There are two ways to do it
mMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener( ) {
#Override
public void onInfoWindowClick(Marker marker) {
Toast.makeText(StoreMap.this, "Info window clicked", Toast.LENGTH_SHORT).show();
}
or use this one if you are intend to direct user just by clicking on marker.
GoogleMap.setOnMarkerClickListener(OnMarkerClickListener)
just implement as similar as above
more details
So you want your app to search for places? Try Geocoding. Here's my super simplified search method:
public void onSearch(String searchLocation) {
mGoogleMap.clear();
List<Address> addressList = null;
Address address = null;
LatLng latLng = null;
Geocoder geocoder = new Geocoder(mContext);
try {
addressList = geocoder.getFromLocationName(searchLocation, 1);
} catch (IOException e) {
e.printStackTrace();
}
address = addressList.get(0);
latLng = new LatLng(address.getLatitude(), address.getLongitude());
mGoogleMap.addMarker(new MarkerOptions().position(latLng).title(searchLocation));
mGoogleMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
}
I want to use Google map in my project. It is working properly in one project but when I use same code in another project map is not displayed on screen. Below is the part of my code:
private class GeocoderTask extends AsyncTask<String, Void, List<Address>>{
#Override
protected List<Address> doInBackground(String... locationName) {
// Creating an instance of Geocoder class
Geocoder geocoder = new Geocoder(getBaseContext());
List<Address> addresses = null;
try {
// Getting a maximum of 3 Address that matches the input text
addresses = geocoder.getFromLocationName(locationName[0], 3);
} catch (IOException e) {
e.printStackTrace();
}
return addresses;
}
#Override
protected void onPostExecute(List<Address> addresses) {
if(addresses==null || addresses.size()==0){
Toast.makeText(getBaseContext(),
"No Location found", Toast.LENGTH_SHORT).show();
}
// Clears all the existing markers on the map
googleMap.clear();
// Adding Markers on Google Map for each matching address
for(int i=0;i<addresses.size();i++){
Address address = (Address) addresses.get(i);
// Creating an instance of GeoPoint, to display in Google Map
latLng = new LatLng(address.getLatitude(), address.getLongitude());
String addressText = String.format("%s, %s",
address.getMaxAddressLineIndex() > 0
? address.getAddressLine(0) : "",
address.getCountryName());
markerOptions = new MarkerOptions();
markerOptions.position(latLng);
markerOptions.title(addressText);
googleMap.addMarker(markerOptions);
// Locate the first location
if(i==0)
googleMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
}
}
}
You have to make new project on Google Console and have to create another API key for your new project. Then use this API key in your new project.
It worked for me.
You have to create new API key for your project.
Here is steps to generate key.
https://developers.google.com/maps/documentation/android/start
Hope this helps you.
You need to get the API key with the package name of your app, this will load the map properly.