我需要在不显示活动地图的情况下获取我的位置!现在,我可以获取位置将mapFragment添加到Layout中。
这是我的代码,请帮助我:
# Using with statement just for good form; in this case it would work the
# same on CPython, but on other interpreters or different CPython use cases,
# it's easy to screw something up; use with statements all the time to build good habits
with open(path, encoding='utf-8') as f:
records=[json.loads(line) for line in f]
答案 0 :(得分:0)
如果您只想要没有地图的位置,则不需要google-maps。
试试这个 - 在您的清单中添加这些权限 -
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
然后在你的代码中使用 -
//get latitude & longitude
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double longitude = location.getLongitude();
double latitude = location.getLatitude();
//get a location listener
private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
//get the address from lat and long.
Geocoder geoCoder = new Geocoder(mContext, Locale.getDefault());
StringBuilder builder = new StringBuilder();
try {
List<Address> address = geoCoder.getFromLocation(latitude, longitude, 1);
int maxLines = address.get(0).getMaxAddressLineIndex();
for (int i=0; i<maxLines; i++) {
String addressStr = address.get(0).getAddressLine(i);
builder.append(addressStr);
builder.append(" ");
}
String finalAddress = builder.toString(); //This is the complete address.
Log.d(TAG, "Final ADD: " + finalAddress);
} catch (IOException e) {}
catch (NullPointerException e) {}
答案 1 :(得分:0)
' getLastKnownLocation '返回' NULL ',因为没有最后知道的位置。要获取该位置,您可以注册位置更新。但在请求更新之前,您的应用必须连接到位置服务。通过调用“ requestLocationUpdates ”来启动更新。在Google客户端提供的“ onConnected()'回调中执行此操作。
有关详情,请以此为参考:http://developer.android.com/training/location/receive-location-updates.html