Google map is displaying but not loading map and directions in android - android

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.

Related

get all the information from a marker google maps-android studio

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)

place markers stores(pirtucular store name) with google maps in android

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!

How to make a marker to a location in Google Maps Activity?

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));
}

Zoom in map different for city vs country android

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);

How to show a specific location on map in android using Google Map Android API v2?

I have this problem.
I want to show a specific location on maps in android. Like if user has entered India in Edit text, then how can i show INDIA on Map. I basically want to know how to get the latitude and longitude of any location.
Please help
My Code:
public class Map extends Activity{
private GoogleMap map;
private LatLng myLoc;
#SuppressLint("NewApi") protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map);
Intent i=getIntent();
String loc=i.getStringExtra("loc");
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
Geocoder geoCoder = new Geocoder(getBaseContext(), Locale.getDefault());
try {
List<Address> addresses = geoCoder.getFromLocationName(
loc, 5);
if (addresses.size() > 0) {
myLoc = new LatLng(
(int) (addresses.get(0).getLatitude() * 1E6),
(int) (addresses.get(0).getLongitude() * 1E6));
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(myLoc, 14);
map.animateCamera(update);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
It is not Working. Please Help
It seems that Geocoder functions are blocked and use network. Because of onCreate is running on main thread, you can get this error(in older versions of android). Thus, you have to create AsyncTask and move geocoder's function to AsyncTask.doInBackground().
And after, in AsyncTask.onPostExecute() you should perfom the other operations with google map. (It will be on main thread in this case, how it must be)
Use
map.setMyLocationEnabled(true);
A blue circle will be appeared on map. It will show the current location.
When you select India from edittext call this function:
public void showSelectedCountry(String countryName) {
if (countryName.equals("India")) {
LatLngBounds boundsIndia = new LatLngBounds(new LatLng(23.63936, 68.14712), new LatLng(28.20453, 97.34466));
int padding = 0; // offset from edges of the map in pixels
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngBounds(boundsIndia, padding);
mMap.animateCamera(cameraUpdate);
}
}

Categories

Resources