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.
Related
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));
}
What I want to do is just show driving routes to my Fragment Map when the Marker is Clicked.
This Tutorial is my source.
Right now the route only shows on OTHER app not in the myactivity layout. What I want to do is show the driving direction route in my fragment
Here is my MainActivity.Class
public class MainActivity extends ActionBarActivity implements GoogleMap.OnMarkerClickListener {
private LatLng Mark_Kantor_Gubernur = new LatLng(-0.9376155, 100.3600001);
private LatLng Mark_Bappeda_prov_Sumbar = new LatLng(-0.913884, 100.359176);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Getting Map From Google
SupportMapFragment supportMapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.googleMap);
googleMap = supportMapFragment.getMap();
googleMap.setMyLocationEnabled(true);
googleMap.getUiSettings().setCompassEnabled(true);
googleMap.getUiSettings().setZoomControlsEnabled(true);
googleMap.setTrafficEnabled(true);
//Getting Location Based on Fused Location(GPS & Network)
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String bestProvider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(bestProvider);
double latitude = location.getLatitude();
double longitude = location.getLongitude();
LatLng latLng = new LatLng(latitude, longitude);
//Handle map when opened on first time
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
googleMap.animateCamera(CameraUpdateFactory.zoomTo(13));
//Setting UP Marker
googleMap.addMarker(new MarkerOptions().position(Mark_Kantor_Gubernur)
.title("Kantor Gubernur")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_cp)));
googleMap.addMarker(new MarkerOptions().position(Mark_Bappeda_prov_Sumbar)
.title("Kantor Bappeda SUMBAR")
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.marker_cp)));
TextView locationTV = (TextView) findViewById(R.id.tvLatLongLocation);
locationTV.setText("Latitude:" + latitude + ",Longitude:" + longitude);
//Setting onclick marker & Direction to Marker
googleMap.setOnMarkerClickListener(this);
}
#Override
public boolean onMarkerClick(Marker marker) {
//you can handle marker click from here
try {
StringBuilder stringBuilder = new StringBuilder();
String daddr = (String.valueOf(marker.getPosition().latitude)+","+String.valueOf(marker.getPosition().longitude));
stringBuilder.append("http://maps.google.com/maps?f=d&hl=en");
stringBuilder.append("&saddr="+String.valueOf(googleMap.getMyLocation().getLatitude())+","
+String.valueOf(googleMap.getMyLocation().getLongitude()));
stringBuilder.append("&daddr="+daddr);
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(stringBuilder.toString()));
startActivity(intent);
}catch (Exception ee)
{
Toast.makeText(getApplicationContext(), "Lokasi Saat Ini Belum Didapatkan, Coba Nyalakan GPS, Keluar Ruangan dan Tunggu Beberapa Saat", Toast.LENGTH_LONG).show();
}
return false;
}
I have looked at J2ME/Android/BlackBerry - Driving directions, Route Between Two Locations, Draw Route On Google Map but this is showing just straight lines between the users location and destination, what I want is driving route.
If I add this polyline code:
Polyline polyline = googleMap.addPolyline(new PolylineOptions().add(new LatLng(googleMap.getMyLocation().getLatitude(), googleMap.getMyLocation().getLongitude()),new LatLng(marker.getPosition().latitude, marker.getPosition().longitude)).width(2).color(Color.RED).geodesic(true));
It just shows straight lines in a Google map fragment
Note: Please include comments and an explanation on answer code, I'm very new in android and still learning.
Selector elements should be placed in res\drawable. Right click on 'drawable', select 'new' then select 'Drawable resource file'. Name the file and you should be good to go.
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
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);
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I want to show a Google map v2 in my android app, I want to get city name on Google map V2. As i enter city name in edittext control. I have one EditText, if i enter the area name or city name in EditText, the map should show the passing area name into my google map.I am using Google Map v2.Any help will be appreciated.
This is my code on button search click.
btSearch.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
try {
Geocoder geocoder = new Geocoder(CreateReminderActivity.this);
List<Address> addresses;
String areaname = edLocation.getText().toString();
addresses = geocoder.getFromLocationName(areaname, 1);
if(addresses.size() > 0) {
double latitude= addresses.get(0).getLatitude();
double longitude= addresses.get(0).getLongitude();
insLat = latitude;
insLong = longitude;
String statename = addresses.get(0).getAdminArea();
googleMap.addMarker(new MarkerOptions()
.title(areaname)
.snippet("SNIPPET")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher))
.anchor(0.0f, 1.0f) // Anchors the marker on the bottom left
.position(new LatLng(latitude, longitude)));
LatLng latLng = new LatLng(latitude, longitude);
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
// Zoom in the Google Map
googleMap.animateCamera(CameraUpdateFactory.zoomTo(10));
Toast.makeText(getApplicationContext(), statename, 20).show();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
Have you checked https://developers.google.com/places/documentation/search ?
You seem to be asking a very basic question.
Check this: How to Search in Google Map Api V2 Android?
You can use Geocoder for this purpose.
try {
Geocoder geocoder = new Geocoder(this);
List<Address> addresses;
addresses = geocoder.getFromLocationName("PLACE_NAME", 1);
if(addresses.size() > 0) {
double latitude= addresses.get(0).getLatitude();
double longitude= addresses.get(0).getLongitude();
map.addMarker(new MarkerOptions()
.title("TITLE")
.snippet("SNIPPET")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher))
.anchor(0.0f, 1.0f) // Anchors the marker on the bottom left
.position(new LatLng(latitude, longitude)));
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}