问题
I have a requirement to loading a Google map location given the pin code or area code. I've tried using the Geocoder method to find the latitude and longitude using a given address. This works when a location or area is given, but is not working for the pincode (specifically India). Is there is any method or way to find the latitude and longitude of a given area using the pincode.
This is specific to a map to be loaded on android, using the mapview.
The code i've written is given as:
package com.example.geocodingexample;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.LinearLayout;
import android.widget.Toast;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
public class MainActivity extends MapActivity {
MapView mapView;
MapController mapController;
GeoPoint p;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mapView = (MapView) findViewById(R.id.mapview);
mapController = mapView.getController();
mapView.setBuiltInZoomControls(true);
mapController = mapView.getController();
mapController.setZoom(14);
Geocoder geoCoder = new Geocoder(this, Locale.getDefault());
try {
List<Address> addresses = geoCoder.getFromLocationName("karnataka",
1);
String add = "";
if (addresses.size() > 0) {
p = new GeoPoint((int) (addresses.get(0).getLatitude() * 1E6),
(int) (addresses.get(0).getLongitude() * 1E6));
mapController.animateTo(p);
mapView.invalidate();
Log.i("latitude obtained", String.valueOf(p.getLatitudeE6()));
Log.i("longitude obtained", String.valueOf(p.getLongitudeE6()));
Toast.makeText(this, "lat obtained", Toast.LENGTH_SHORT).show();
}
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
回答1:
For India it will not work by Pincode.
But try this alternate method. you can add additional arguments, such as "Near=", perhaps you can add Indian states to the query and make it work.
This may work but i am not sure. Have fun
回答2:
It works when you increase the number of location in method signature.
getFromLocationName("karnataka",
5);
it will give you latitude and longitude. Ex: -
public void findLocationName()
{
try {
Geocoder gcd = new Geocoder(this, Locale.getDefault());
List<Address> addresses;
addresses = gcd.getFromLocationName("110002",5);
for(int i=0;i<addresses.size();i++){
Log.e("","in loop");
Log.e("","lat :,,,,,,,,,,,,,"+addresses.get(i).getLatitude()+" long............"+addresses.get(i).getLongitude());
Toast.makeText(this, "lat : "+addresses.get(i).getLatitude(),1).show();
Toast.makeText(this, "long : "+addresses.get(i).getLongitude(),1).show();
}
}
catch (IOException e) {e.printStackTrace();}
}
来源:https://stackoverflow.com/questions/13226721/obtain-the-latitude-and-longitude-using-the-pincode-on-google-maps