android– 缩放显示的当前用户位置

android– 缩放显示的当前用户位置,第1张

概述我正在使用GoogleMapsforAndroidv2.我想显示当前用户位置并放大它.这是FragmentActivity中的代码:@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.map_activity);mMap=((Sup

我正在使用Google Maps for Android v2.我想显示当前用户位置并放大它.
这是FragmentActivity中的代码:

@OverrIDeprotected 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));    }}

地图工作正常,当前位置的蓝点也可以工作.但似乎currentLocation始终为null.所以我无法放大当前位置.

谁知道为什么,拜托?

解决方法:

以下是getMyLocation()的实现,其中包含用于查找人员位置的附加保护.我只是在管理地图片段的活动中有这个.

private Location getMyLocation() {    // Get location from GPS if it's available    LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);    Location myLocation = lm.getLastKNownLocation(LocationManager.GPS_PROVIDER);    // Location wasn't found, check the next most accurate place for the current location    if (myLocation == null) {        Criteria criteria = new Criteria();        criteria.setAccuracy(Criteria.ACCURACY_COARSE);        // Finds a provIDer that matches the criteria        String provIDer = lm.getBestProvIDer(criteria, true);        // Use the provIDer to get the last kNown location        myLocation = lm.getLastKNownLocation(provIDer);    }    return myLocation;}

谢谢,
DMAN

总结

以上是内存溢出为你收集整理的android – 缩放显示的当前用户位置全部内容,希望文章能够帮你解决android – 缩放显示的当前用户位置所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1099285.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存