简体   繁体   English

如何从地址中查找经纬度

[英]How to find Latitude and Longitude from Address

I am using Address to get Latitude and Longitude of that location, but it always returns null... 我正在使用地址获取该位置的纬度和经度,但它始终返回null。

I am passing address like this: 1701 Amphitheatre Pkwy Mountain View CA 94043 我正在通过这样的地址: 1701 Amphitheatre Pkwy Mountain View CA 94043

Geocoder geocoder = new Geocoder(MainActivity.this, Locale.getDefault());
try {                                                
    List<Address> addresses = geocoder.getFromLocationName(strNewClientCompleteAddress, 1);
    if ((addresses != null) && (addresses.size() > 0)) {
        Address fetchedAddress = addresses.get(0);
        lat = fetchedAddress.getLatitude();
        lng = fetchedAddress.getLongitude();
        Log.v("try-if", "ok great work");
    } else {
        Log.v("try-else", "something wrong");
    }
} catch (IOException e) {
    e.printStackTrace();
    Log.v("catch", "Could not get address....!");
}                           
strLat = String.valueOf(lat);
Log.v("lat:", strLat);
strLng = String.valueOf(lng);
Log.v("lng:", strLng);

and Log says 日志

V/strNewClientCompleteAddress:(1466): 1701 Amphitheatre Pkwy Mountain View CA 94043
V/try-else(1466): something wrong
V/lat:(1466): 0.0
V/lng:(1466): 0.0

Manifest permissions : 清单权限

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

I checked your code, there nothing is wrong in code. 我检查了您的代码,代码没有任何错误。

I got below log. 我低于日志。

02-21 06:21:16.036: V/try-if(18306): ok great work
02-21 06:21:16.036: V/lat:(18306): 37.4235981
02-21 06:21:16.036: V/lng:(18306): -122.0871014

Please make sure you have import below class. 请确保您已导入以下课程。

import android.location.Address;
import android.location.Geocoder;

The Geocoder class backend is only present on Google-approved devices. Geocoder类的后端仅存在于Google批准的设备上。 So the lookup will basically fail on any device that doesn't have GMail/Market/Maps on it. 因此,在没有GMail / Market / Maps的任何设备上,查找基本上将失败。

Also check is internet is working in device.. 还检查互联网是否正在设备中工作。

Note : Sometime Geocoder service is not working in background because its killed due to less memory or task manager. 注意:有时Geocoder服务无法在后台运行,因为由于内存或任务管理器较少而导致服务被终止。 It will start again after reboot device. 重新启动设备后,它将再次启动。

  1. Have you set the necessary permissions, eg: 您是否设置了必要的权限,例如:

    android.permission.INTERNET" android.permission.INTERNET对”

  2. Have you added Google Play Services? 您添加了Google Play服务吗?

  3. What return Geocoder.isPresent() method? 什么返回Geocoder.isPresent()方法? http://developer.android.com/reference/android/location/Geocoder.html#isPresent%28%29 http://developer.android.com/reference/android/location/Geocoder.html#isPresent%28%29

Another way is to use Google Api https://developers.google.com/places/documentation/autocomplete 另一种方法是使用Google Api https://developers.google.com/places/documentation/autocomplete

directly, you will get JSON list with locations. 直接,您将获得带有位置的JSON列表。

this approach is not dependent on Play Services on phone, but uses Maps API Key 这种方法不依赖于手机上的Play服务,而是使用Maps API Key

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM