implementing the requestlocationchange in google maps - android

In my application, I have google maps and I'm currently showing my location using gps but i have a user input field where user can give their location, based on the user input my location should change to user location

You can use a CameraUpdate to change the position
LatLng latLng = user's position
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLng(latLng)
map.moveCamera(cameraUpdate);
if the user inputs an address and not the coordinates (which is very likely) you can use a geocoder to get the coordinates from the address
Geocoder coder = new Geocoder(this);
List<Address> address;
try {
address = coder.getFromLocationName(strAddress,5);
if (address == null) {
return null;
}
Address location = address.get(0);
location.getLatitude();
location.getLongitude();
}

Related

all areas latitude and longitude between route defined

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.

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

App Crashes when entering invalid or null location using geocoder

I have been working on google maps and want the user can access location using geocoding, although it is working but the problem arises when the user enters invalid location or null location in edit text, my app gets crashed.
This is my code: (on search button click)
public void onSearch(View view) {
EditText addressSearch = (EditText) findViewById(R.id.edtSearchAddress);
String location = addressSearch.getText().toString();
List<Address> addressList = null;
if (location != null || !location.equals("")) {
addressSearch.setText("");
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
try {
addressList= geocoder.getFromLocationName(location, 7);
} catch (IOException e) {
e.printStackTrace();
}
// location exists
Address address = addressList.get(0);
LatLng latLng = new LatLng(address.getLatitude(), address.getLongitude());
map.addMarker(new MarkerOptions().position(latLng).title("Find Pro"));
map.animateCamera(CameraUpdateFactory.newLatLng(latLng));
} else {
addressSearch.setText("Location does not exist");
}
}
Your kind support will be appreciated.
According to documentation the call of geocoder.getFromLocationName can throw IllegalArgumentException or return empty list. Both cases will crash your app. My bet the list is empty.
So guard your code:
if (addressList.size() > 0) {
Address address = addressList.get(0);
LatLng latLng = new LatLng(address.getLatitude(), address.getLongitude());
map.addMarker(new MarkerOptions().position(latLng).title("Find Pro"));
map.animateCamera(CameraUpdateFactory.newLatLng(latLng));
}
when you pass null value that time addressList return 0 value so please set IF condition before get value from addressList.
if (addressList.size() > 0) {
Address address = addressList.get(0);
}

Using Maps in Android

I have the address to certain location in terms of a string. However i want to convert this address to Lattitude and Longitude. So i used the code given below to convert into geopoint. But this geopoint is not getting added to the map. The map always shows the default map thats the entire world.
public GeoPoint getLocationFromAddress(String strAddress) {
Geocoder coder = new Geocoder(this);
List<Address> address;
GeoPoint p1 = null;
try {
address = coder.getFromLocationName(strAddress, 5);
if (address == null) {
return null;
}
Address location = address.get(0);
location.getLatitude();
location.getLongitude();
p1 = new GeoPoint((int) (location.getLatitude() * 1E6),
(int) (location.getLongitude() * 1E6));
return p1;
} catch (IOException e) {
e.printStackTrace();
return p1;
}
}
Im new with maps and am not sure what Im doing wrong. Any suggestions?
Now that you know the latitude and longitude, you forgot to update the map to the specific location. You can do it like this:
int zoomNiveau = 15;
map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()), zoomNiveau));
With this code, the map will go to the location of the address. But you need to create a Marker and add it to the map if you want a Marker.
More info about the markers: https://developers.google.com/maps/documentation/android/marker

How to get coordinates of an address in android

How do i get gps co-ordinates of the location/address entered by user in android ?
Geocoder geocoder = new Geocoder(<your context>);
List<Address> addresses;
addresses = geocoder.getFromLocationName(<String address>, 1);
if(addresses.size() > 0) {
double latitude= addresses.get(0).getLatitude();
double longitude= addresses.get(0).getLongitude();
}
You can use Android's Geocoder to do reverse geocoding:
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocationName(myLocation, 1);
Address address = addresses.get(0);
double longitude = address.getLongitude();
double latitude = address.getLatitude();
Also include the following in AndroidManifest.xml:
<uses-permission android:name="android.permission.INTERNET"/>
Also note that you need to be using an API which includes a Geocoder implementation. APIs which include this are the Android Google APIs for example. You can use Geocoder.isPresent() to check if an implementation exists for your targeted API.
Check out the Geocoder documentation for more information.
List<Address> addresses;
addresses = geocoder.getFromLocationName(<String address>, 1);
if(addresses.size() > 0){
double latitude= addresses.get(0).getLatitude();
double longitude= addresses.get(0).getLongitude();
}
manifest permissions:-
android.permission.INTERNET
android.permission.ACCESS_COARSE_LOCATION
android.permission.ACCESS_FINE_LOCATION
android.permission.ACCESS_MOCK_LOCATION
use manifest permissions like
android.permission.INTERNET
android.permission.ACCESS_COARSE_LOCATION
android.permission.ACCESS_FINE_LOCATION
android.permission.ACCESS_MOCK_LOCATION
and go with this
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocationName(myLocation, 1);
Address address = addresses.get(0);
if(addresses.size() > 0) {
double latitude = addresses.get(0).getLatitude();
double longitude = addresses.get(0).getLongitude();
}
Here is a complete sample
First define a zoom
final CameraUpdate zoom = CameraUpdateFactory.zoomTo(5);
Clear any map markers
mMap.clear();
//Declare a new marker
final MarkerOptions mp = new MarkerOptions();
//declare a EditText to get the user informed address
EditText etEndereco = findViewById(R.id.map_prof_etEnd);
Geocoder geocoder = new Geocoder(map_prof.this);
List<Address> addresses = null;
try {
addresses = geocoder.getFromLocationName(etEndereco.getText().toString(), 1);
} catch (IOException e) {
e.printStackTrace();
}
if (addresses.size() > 0) {
double latitude = addresses.get(0).getLatitude();
double longitude = addresses.get(0).getLongitude();
mp.position(new LatLng(latitude, longitude));
Log.e("teste map2", "inserted latitude " + Latitude + ", inserted Longitude " + Longitude);
mp.title("Selected location");
CameraUpdate center = CameraUpdateFactory.newLatLng(new LatLng(latitude, longitude));
mMap.clear();
mMap.addMarker(mp);
mMap.moveCamera(center);
mMap.animateCamera(zoom);
}

Categories

Resources