我正在尝试使用新的谷歌地图android api v2,我正在为Android 2.3.3或更高版本开发一个应用程序.这是一个非常简单的应用程序:它使用方向api从db获取POI的用户当前位置(使用gps或网络信号),它将用户驱动到POI.
我的问题是获取用户当前位置.感谢这篇文章 如何获取Google Maps Android API v2中的当前位置?我了解到我无法使用新的谷歌API来更新当前位置.另一个问题是我可以设置我的位置,GoogleMap setMyLocationEnabled(boolean enabled)
但我无法getMyLocation()
知道用户在哪里.
我使用本指南http://www.vogella.com/articles/AndroidLocationAPI/article.html#maps_mylocation获取我的位置,并尝试在我的Activity中集成以绘制用户位置.
我的代码:
表现
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="it.mappe"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="it.mappe.permission.MAPS_RECEIVE" />
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="MYGMAPSKEY" />
<activity
android:name="it.mappe.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity> …
Run Code Online (Sandbox Code Playgroud) 我正在使用Google Maps for Android v2.我想显示当前用户位置并放大它.这是以下代码FragmentActivity
:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map_activity);
mMap = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.mapFragment_mapActivity)).getMap();
if (mMap == null) {
Toast.makeText(this, R.string.mapCreationProblem, Toast.LENGTH_LONG)
.show();
finish();
return;
}
mMap.setMyLocationEnabled(true);
Location currentLocation = mMap.getMyLocation();
if(currentLocation!=null){
LatLng currentCoordinates = new LatLng(
currentLocation.getLatitude(),
currentLocation.getLongitude());
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(currentCoordinates, 10));
}
}
Run Code Online (Sandbox Code Playgroud)
地图工作正常,当前位置的蓝点也可以工作.但似乎currentLocation
总是空的.所以我无法放大当前位置.
谁知道为什么,拜托?
我想在Google Maps Android API v2中启用位置.我想要的是:
我对如何定期更新,选择时间间隔的数量感到困惑.现在,我已经设置了项目来显示地图并使用
googleMap.setMyLocationEnabled(true);
googleMap.setOnMyLocationChangeListener(myLocationChangeListener);
private GoogleMap.OnMyLocationChangeListener myLocationChangeListener = new GoogleMap.OnMyLocationChangeListener() {
@Override
public void onMyLocationChange(Location location) {
LatLng loc = new LatLng(location.getLatitude(), location.getLongitude());
if(googleMap != null){
if(!firstTime){
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(loc).zoom(zoom).build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
firstTime = true;
}
lat = location.getLatitude();
lng = location.getLongitude();
Toast.makeText(context, "Location is: " + String.valueOf(lat) + ", "
+ String.valueOf(lng), Toast.LENGTH_LONG).show();
}
}
};
Run Code Online (Sandbox Code Playgroud)
在变量中设置lat/lng.我的问题是:
我找到了一些方法来做到这一点,但已被弃用或无效.我想从设备获得当前的经度和经度.
以下是我获取当前位置的getMyLocation()
方法,但不推荐使用 GoogleMap 方法:
void getCurrentLocation()
{
Location myLocation = map.getMyLocation();
if(myLocation!=null)
{
double dLatitude = myLocation.getLatitude();
double dLongitude = myLocation.getLongitude();
map.addMarker(new MarkerOptions().position(new LatLng(dLatitude, dLongitude))
.title("My Location").icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(dLatitude, dLongitude), 8));
}
else
{
Toast.makeText(this, "Unable to fetch the current location", Toast.LENGTH_SHORT).show();
}
}
Run Code Online (Sandbox Code Playgroud)