【发布时间】:2017-05-12 12:18:28
【问题描述】:
我正在尝试使用 GPS 获取用户的当前位置。
下面是我的 initialize() 方法,它在获得所有必需的权限后被调用。我正在尝试使用此处地图文档中指定的这条线获取用户的地理位置
PositioningManager posManager = PositioningManager.getInstance();
posManager.start(PositioningManager.LocationMethod.GPS);
GeoPosition position = posManager.getPosition();
现在我的问题是位置总是返回null,不确定我哪里出错了。
initialize() 方法:
private void initialize() {
setContentView(R.layout.activity_main);
// Search for the map fragment to finish setup by calling init().
mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.mapfragment);
mapFragment.init(new OnEngineInitListener() {
@Override
public void onEngineInitializationCompleted(OnEngineInitListener.Error error) {
if (error == OnEngineInitListener.Error.NONE) {
// retrieve a reference of the map from the map fragment
map = mapFragment.getMap();
// Set the map center coordinate to the Vancouver region (no animation)
map.setCenter(new GeoCoordinate(19.174598, 72.860722),
Map.Animation.NONE);
// Set the map zoom level to the average between min and max (no animation)
map.setZoomLevel((map.getMaxZoomLevel() + map.getMinZoomLevel()) / 2);
MapMarker marker = new MapMarker();
PositioningManager posManager = PositioningManager.getInstance();
posManager.start(PositioningManager.LocationMethod.GPS_NETWORK);
posManager.start(PositioningManager.LocationMethod.GPS);
// GeoPosition position = posManager.getPosition();
Log.d(LOG_TAG, "onEngineInitializationCompleted: " + posManager.hasValidPosition(PositioningManager.LocationMethod.GPS_NETWORK));
TextView info = (TextView) findViewById(R.id.info);
boolean temp = posManager.hasValidPosition(PositioningManager.LocationMethod.GPS_NETWORK);
info.setText(Boolean.toString(temp));
marker.setCoordinate(new GeoCoordinate(19.1745, 72.8));
map.addMapObject(marker);
} else {
Log.e(LOG_TAG, "Cannot initialize MapFragment (" + error + ")");
}
}
});
textViewResult = (TextView) findViewById(R.id.title);
textViewResult.setText(R.string.textview_routecoordinates_2waypoints);
}
Android 清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mapstutorial.simplerouting" >
<!-- <uses-sdk android:minSdkVersion="15" android:targetSdkVersion="23"/>
No longer needed since this is specified in build.gradle -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<application
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name">
<activity
android:name=".RoutingActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.here.android.maps.appid"
android:value="bm8G03BysQAjEHvyCgSm" />
<meta-data
android:name="com.here.android.maps.apptoken"
android:value="CprS7w8rXTBHblaSlb8xVQ" />
</application>
</manifest>
这里是Github Gist 的链接以及完整的java 代码。谁能告诉我我做错了什么。
这段java代码只是对Here map的sdk示例稍作修改。
【问题讨论】:
-
您使用哪种设备进行测试?也许它没有 GPS 天线?
-
乐视1s,安卓棉花糖。
-
类似:stackoverflow.com/questions/43833725/…,你可以在那里看到一些调试建议。
-
如那里讨论的那样,您说
posManager.start(PositioningManager.LocationMethod.GPS_NETWORK);返回true 仍然posManager.hasValidPosition(PositioningManager.LocationMethod.GPS_NETWORK);返回null。我是否必须仅在addListener方法中获取当前位置的位置?? -
是因为我在 sdk 示例中使用的默认
appid和apptoken吗?
标签: android gps here-api android-gps