我想在每次位置更改时在地图中放置一个标记。我从Location
中获取纬度和长度,并在onLocationChanged()
方法中创建标记。为什么标记不被创建?在android上抓取谷歌地图v2的用户位置?
public class MainActivity extends FragmentActivity implements LocationListener
{
Context context = this;
GoogleMap googlemap;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initMap();
addTwittertoMap();
LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 0,
this);
String provider = lm.getBestProvider(new Criteria(), true);
}
public void onLocationChanged(Location location) {
LatLng current = new LatLng(location.getLatitude(), location.getLatitude());
Date date = new Date();
googlemap.addMarker(new MarkerOptions()
.title("Current Pos")
.snippet(new Timestamp(date.getTime()).toString())
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE))
.position(current)
);
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
public void onProviderEnabled(String provider) {
}
public void onProviderDisabled(String provider) {
}
private void initMap(){
SupportMapFragment mf = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
googlemap = mf.getMap();
googlemap.setMyLocationEnabled(true);
googlemap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
}
你确保你能获得位置更新? – iagreen 2013-02-10 02:07:19
是的,我已经在外面测试过它,并且驱动它并更新。 – 2013-02-11 00:29:32
我会放大检索的位置来检查。因此,在你的'onLocationChanged'中,放大位置以确保不添加标记。 – 2013-02-15 14:16:09