我正在尝试获取位置或想要通过传递位置名称在地图上显示位置 我做了一个示例项目,在地图上显示当前位置。但我不明白如何在谷歌地图上传递名称和搜索位置 这是我的代码,可以帮助您理解我的问题。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
mapView.setSatellite(true);
mapController = mapView.getController();
mapController.setZoom(14); // Zoom 1 is world view
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
0, new GeoUpdateHandler());
myLocationOverlay = new MyLocationOverlay(this, mapView);
mapView.getOverlays().add(myLocationOverlay);
String value = "kasarwadi,pune";
// Do something with value!
Geocoder geoCoder = new Geocoder(getBaseContext(), Locale.getDefault());
try {
List<Address> addresses = geoCoder.getFromLocationName(
value, 5);
String add = "";
if (addresses.size() > 0) {
GeoPoint p = new GeoPoint(
(int) (addresses.get(0).getLatitude() * 1E6),
(int) (addresses.get(0).getLongitude() * 1E6));
mapController.animateTo(p);
mapView.invalidate();
}
} catch (IOException e) {
e.printStackTrace();
}
myLocationOverlay.runOnFirstFix(new Runnable() {
public void run() {
mapView.getController().animateTo(myLocationOverlay.getMyLocation());
}
});
Drawable drawable = this.getResources().getDrawable(R.drawable.point);
itemizedoverlay = new MyOverlays(this, drawable);
createMarker();
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
public class GeoUpdateHandler implements LocationListener {
@Override
public void onLocationChanged(Location location) {
String value = "kasarwadi,pune";
// Do something with value!
Geocoder geoCoder = new Geocoder(getBaseContext(), Locale.getDefault());
try {
List<Address> addresses = geoCoder.getFromLocationName(
value, 5);
String add = "";
if (addresses.size() > 0) {
GeoPoint p = new GeoPoint(
(int) (addresses.get(0).getLatitude() * 1E6),
(int) (addresses.get(0).getLongitude() * 1E6));
mapController.animateTo(p);
mapView.invalidate();
}
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
private void createMarker() {
GeoPoint p = mapView.getMapCenter();
OverlayItem overlayitem = new OverlayItem(p, "", "");
itemizedoverlay.addOverlay(overlayitem);
if (itemizedoverlay.size() > 0) {
mapView.getOverlays().add(itemizedoverlay);
}
}
@Override
protected void onResume() {
super.onResume();
myLocationOverlay.enableMyLocation();
myLocationOverlay.enableCompass();
}
@Override
protected void onPause() {
super.onPause();
myLocationOverlay.disableMyLocation();
myLocationOverlay.disableCompass();
}
我还生成MD5指纹和有效的api密钥。但当我试图显示它只显示当前位置但我需要显示位置由我传递的名称 请给我一个参考或提示 提前谢谢。
答案 0 :(得分:0)
因为您要求最多5个地址
List<Address> addresses = geoCoder.getFromLocationName(value, 5);
第五个或最后一个位置纬度和经度有可能与第一个值完全不同(更合适)。
并且您正尝试动画到最后一个位置。尝试动画到第一个位置。
答案 1 :(得分:0)
我在我的模拟器上遇到了类似的问题,可能它最终会在设备上工作(尚未在设备上测试它!)所以我如何解决这个问题是通过使用另一个原始api将地址转换为geopoint。 ..
所以,我建议不要删除或更改您当前的代码(因为它对我来说很好,谁知道最终可能会在设备方案中工作),但是在catch(IOException)中调用此方法tryAlternative()条款。我正在写下tryAlternative()
private void tryAltesrnative(String address) {
String strCompleteAddress = address.replaceAll(" ", "%20");
String streetName = strCompleteAddress.substring(strCompleteAddress.indexOf(",")+1);
String cityName = streetName.substring(streetName.indexOf(",")+1);
String addressToToast = "";
HttpGet httpGet = new HttpGet("http://maps.google." + "com/maps/api/geocode/json?address=" + address + "ka&sensor=false");
HttpClient client = new DefaultHttpClient();
HttpResponse response;
StringBuilder stringBuilder = new StringBuilder();
try {
response = client.execute(httpGet);
HttpEntity entity = response.getEntity();
InputStream stream = entity.getContent();
int b;
while ((b = stream.read()) != -1) {
stringBuilder.append((char) b);
}
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
JSONObject jsonObject = new JSONObject();
try {
jsonObject = new JSONObject(stringBuilder.toString());
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Double lon = new Double(0);
Double lat = new Double(0);
try {
lon = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
.getJSONObject("geometry").getJSONObject("location")
.getDouble("lng");
lat = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
.getJSONObject("geometry").getJSONObject("location")
.getDouble("lat");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
point = new GeoPoint((int) (lat * 1E6), (int) (lon * 1E6));
mapController.animateTo(point);
mapController.setZoom(12);
List<Overlay> mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.mark_green);
AddItemizedOverlay itemizedOverlay = new AddItemizedOverlay(drawable, this);
OverlayItem overlayitem = new OverlayItem(point, "Customer", addressToToast);
itemizedOverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedOverlay);
mapView.invalidate();
Toast.makeText(this, "Lat: " + point.getLatitudeE6() +", Lon: "+ point.getLongitudeE6() + "Address: " + addressToToast, Toast.LENGTH_LONG).show();
}