我有一个地址名称,我想从我的应用程序中获取纬度和经度,我参考了一些教程,我仍然无法在最后成功获得它。
日志值为空。
我错过了什么步骤?任何帮助都会感激不尽,谢谢。
全局变量:
double editLatitude, editLongitude;
我的地理编码器:
Geocoder coder = new Geocoder(getActivity());
List<Address> address;
try {
address = coder.getFromLocationName("Las Vegas", 5);
if (address == null) {
return null;
}
Address location = address.get(0);
editLatitude = location.getLatitude();
editLongitude = location.getLongitude();
Log.d("editLatitude", editLatitude + ">>>>>>>>>>>>>>>>>>");
Log.d("editLongitude", editLongitude + ">>>>>>>>>>>>>>>>>>");
} catch (IOException ex) {
ex.printStackTrace();
}
我在这里设置纬度和经度:
@Override
public void onLocationChanged(Location location) {
mLastLocation = location;
if (mCurrLocationMarker != null) {
mCurrLocationMarker.remove();
}
//Place current location marker
//LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
//LatLng latLng = new LatLng(22.751262, 121.140801);
//---------------------------------------------
LatLng latLng = new LatLng(editLatitude, editLongitude);
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(latLng);
markerOptions.title("Current Position");
markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
mCurrLocationMarker = mGoogleMap.addMarker(markerOptions);
//move map camera
mGoogleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mGoogleMap.animateCamera(CameraUpdateFactory.zoomTo(16));
//optionally, stop location updates if only current location is needed
if (mGoogleApiClient != null) {
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
}
}
答案 0 :(得分:1)
创建一个方法,返回带有HTTP调用响应的JSONObject,如下所示
public static JSONObject getLocationInfo(String address) {
StringBuilder stringBuilder = new StringBuilder();
try {
address = address.replaceAll(" ","%20");
HttpPost httppost = new HttpPost("http://maps.google.com/maps/api/geocode/json?address=" + address + "&sensor=false");
HttpClient client = new DefaultHttpClient();
HttpResponse response;
stringBuilder = new StringBuilder();
response = client.execute(httppost);
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();
}
return jsonObject;
}
现在将JSONObject传递给getLatLong()方法,如下所示
public static boolean getLatLong(JSONObject jsonObject) {
try {
longitute = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
.getJSONObject("geometry").getJSONObject("location")
.getDouble("lng");
latitude = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
.getJSONObject("geometry").getJSONObject("location")
.getDouble("lat");
} catch (JSONException e) {
return false;
}
return true;
}
答案 1 :(得分:0)
试试这个
double Lat = geocoder.getFromLocationName(address_name, 1).get(0).getLatitude();
double Lon = geocoder.getFromLocationName(address_name, 1).get(0).getLongitude();