
我想在每次位置变化时在地图中放置一个标记.我从位置抓取Lat和Long并在onLocationChanged()方法中创建一个标记.为什么不创建标记?
public class MainActivity extends FragmentActivity implements LocationListener{Context context = this;GoogleMap Googlemap;/** Called when the activity is first created. */@OverrIDepublic 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);}解决方法:
LatLng current = new LatLng(location.getLatitude(),location.getLatitude());应该
LatLng current = new LatLng(location.getLatitude(),location.getLongitude());
另外,作为对LocationManager配置的改进,我建议这样设置:
LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE); String provIDer = lm.getBestProvIDer(new Criteria(), true); lm.requestLocationUpdates(provIDer, 10000, 0, this); 总结 以上是内存溢出为你收集整理的java – 在Android上使用google maps v2抓取用户位置?全部内容,希望文章能够帮你解决java – 在Android上使用google maps v2抓取用户位置?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)