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
Related
I am an entry level programmer and i want to create an app, in which an address converts to a specific latitude and longtitude. Is there an easy way to do this? I read a few things about geocoder. Could you please describe to me, where and how?
This can easily be done with GeoCoder. Here is the code for rescue ;)
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((double) (location.getLatitude() * 1E6),
(double) (location.getLongitude() * 1E6));
return p1;
}
}
where strAddress is the actual address you want long and lat.
Happy Coding :)
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();
}
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);
}
}
I want to show a route between the user's current location and the location he entered in the searchview. Here is my code for the onQueryTextSubmit method
#Override
public boolean onQueryTextSubmit(String query) {
// TODO Auto-generated method stub
try
{
if(direction.isChecked() == false)
{
gc = new Geocoder(this);
addresses = gc.getFromLocationName(query,5);
String add = "";
if (addresses.size() > 0)
{
p = new GeoPoint(
(int) (addresses.get(0).getLatitude() * 1E6),
(int) (addresses.get(0).getLongitude() * 1E6));
mc.animateTo(p);
mc.setZoom(9);
mapView.invalidate();
searchView.clearFocus();
}
else
{
Toast.makeText(this, " Location not found.",
Toast.LENGTH_LONG).show();
}
return true;
}
else
{
// draw a route between the locations
return true;
}
}
catch(IOException ex)
{
}
return false;
}
what is the simplest way to do it? (examples will help me understand more)
Thank You
Once you have your destination GeoPoint you could do an http request to google in this address:
String url = "http://maps.google.com/maps?f=d&hl=es&saddr="+ origin +"&daddr=" + destination + "&ie=UTF8&om=0&output=dragdir";
being origin and destination (latitude, longitude) points.
For example, this request gives you this json:
{tooltipHtml:" (86,4 km / 55 minutos)",polylines:[{id:"route0",points:"wa{uFt|kUoAkE??jDyE??uFuLqFaH??mAl#gAZ??q#[Yg#KwCBkATq#X[bCu#??tMsBrIuBrDoA~J}EbC{#pCy#dIeA`JBrDThd#nFpDRnTkAdBGVFhRyC`McA|KkAzCK|CP??lA~#NZHn#I~#Q`#c#\\[Hu#KYWUe#Gc#D{BnG}a#`Kcb#|A}JvAkOXmHDgFKcMMmDkBgW??SaCEoFBaCb#mH`AwGdE_Qh#sCf#}Db#yGFcFIqE]iGcAkLUyGB{K^aHf#_EfC_PtAqOlB}X|AiNrCyTnE{WjDqOtCqK~DoMhHoSvX}u#~KeYpI}VrSkk#zBiJ`#_Ct#oG`#mGPoGCcLMoDq#wHiCaSe#qE_BgTMuEEoIDsJxA_`#DcHGoCw#iJkAoHsCwN_AcHk#mG]wGOcLLeJZiG\\gEr#kGhBeKpB{H~#uCvB{F~CuGbBuCdE_GlE}ElBgBfQeNpCsCtCmDtCoEnBsD~BmFdBqFz#_DdByIt#iGb#iFZeJBiYJ{GRkGf#{HpAaMdDwRzB{IrCsIrCoGbHiMzGmJnEaFjCeCdCwBfE}CdC_BzHiErNyG~N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oF`AiBnBoCzFwF~CoDlBcEfAaD|Kc_#r#yBtAgD`AaBbHsJ|CsCrP_LpBwB`BqChAgDr#yDR}BhBs\\VuBx#kDlAyCdBkCdDqClQcHlC_B|CuDlJcQbA{AlAqA~_Ags#lSiSrBaCfOiWtVs]z`#_s#zMiVvAuBzPyTfRg[rBuD`B_EtCkIvKs]nCaHlEuIfHaMbMoRv[kd#lEqFfo#qp#|C{Cf#[bQsHrBoA~AsAbAqA|GsMtBiC|B_BlHwD`Au#vA{AnAmB|#wB|#eDbEmThAsEh#_BzAcDnKmP~z#}kA|AcBbDcCvFyBvSgGnBa#~TiGnNmC|DkB??xCoApDU|#UtAeAZm#\\gARkBQeK#i#^cC??pBkHvFy]Pm#ZCHQVuAAc#hAyC~#m#dBYzPq#??Jd#NJR?VSBi#pBq#l#e#d#e#jAoB\\_A\\_BnBePTCN[A[SWtIsr#Aq#VCLSAg#OQpDwX^kE??rAJrB]p#[tAuA",levels:"BBBBB?BB?BB???#??BB??????#???#???????BB??#???#????#???#????BB???#????#??????#??????#??#????#??A???#????#????#?????#????A?????#?????#???#???#?????#???A?#???#????#???#???#???A?????#????#??????????#??????#????#?????#???#???#?????BB??#???#??????#????#??#???#??A???#?????#????#?#??????????#???A???#?????#?????#???#?????#???????#??#???#???#????#???#????#???#???#??#????A????#????#????#?????#???#???#?????#??????#???#???#?????#??#???#????#???#?????#??????A???#???#??#??????#????#??#?##???#???#???#??????#?#??#????BB????#????BB???????#??BB??????#???#????????????BB???B",numLevels:4,zoomFactor:16}]}
The String with name points is the polyline you have to draw on your map. But before drawing you have to decode it. Take a look here for that duty.
Once you have your GeoPoint list, you can draw it on your map. For that you need an overlay if you're using google api v1. I'd recommend you to have a look into api v2. It's easier to add polylines to map objects via the addPolyline method in GoogleMap class and PolylineOptions.
Hope that helps.
by two ways we can draw route on map.
1.use Map OverLay concept in android
2.use this url
http://maps.google.com/maps?&saddr=from latitude value,from longitude value &daddr= to latitude value,to longitude value
In my app I am using the built in MyLocationOverlay on my map.
myLocationOverlay = new MyLocationOverlay(this, mapView){
public void onLocationChanged(Location loc){
super.onLocationChanged(loc);
List<Address> address;
try{
Geocoder mGC = new Geocoder(context,Locale.ENGLISH);
address = mGC.getFromLocation(loc.getLatitude(), loc.getLongitude(), 1);
if (address !=null){
currentAddr = address.get(0);
mHandler.post(updateUI);
}
}catch(IOException e){}
}
};
With the above code I can get the address and visually see where I am. Now i want to add a route overlay from where I am to some destination, how can I implement something like this? Is there some built in overlay for this as well?