简体   繁体   English

Android:在 Google Map 活动上设置用户位置

[英]Android: Setting user location on Google Map activity

i have just made an activity within my app that shows a map to the user.我刚刚在我的应用程序中进行了一项活动,向用户显示了 map。 how can i put the location of the user onto this map?我怎样才能把用户的位置放到这个 map 上? little bit confused about doing this so any help would be appreciated!这样做有点困惑,所以任何帮助将不胜感激!

ALSO: when i set up the map, I had to include: MapView mapView = (MapView) findViewById(R.id.mapview);另外:当我设置 map 时,我必须包括: MapView mapView = (MapView) findViewById(R.id.mapview); Why do you have to include this because don't you already have: setContentView(R.layout.main);?为什么你必须包含这个,因为你还没有:setContentView(R.layout.main);? -- sounds like an idiot question but its just something i wondered! - 听起来像一个白痴问题,但这只是我想知道的!

I'd suggest using the MyLocationOverlay.我建议使用 MyLocationOverlay。 This will get the user's current location and display it on the map via the blue dot.这将获取用户的当前位置并通过蓝点将其显示在 map 上。 You can accomplish this with something like:您可以通过以下方式完成此操作:

List<Overlay> overlays = mMaps.getOverlays();
overlays.clear();

MyLocationOverlay myLocOverlay = new MyLocationOverlay(this, mMaps);
myLocOverlay.enableMyLocation();
overlays.add(myLocOverlay);
myLocOverlay.runOnFirstFix(new Runnable() { public void run() {
    mMaps.getController().animateTo(myLocOverlay.getMyLocation());
}});

Here is a sample code that do the job.这是完成这项工作的示例代码。 Short description, you are first getting locationService to learn location then with Criteria api you lets the system choose best provider according to your settings in Criteria then you got provider, request location updates from provider and implement LocationListener interface, too get cached last location you can call getLastKnownLocation but I wouldnt count on that may lead to outdated or null results简短描述,您首先获取 locationService 以了解位置,然后使用 Criteria api 让系统根据您在 Criteria 中的设置选择最佳提供程序,然后您获得提供程序,从提供程序请求位置更新并实现 LocationListener 接口,也可以缓存最后一个位置调用 getLastKnownLocation 但我不会指望这可能会导致过时或 null 结果

        String contenxt = Context.LOCATION_SERVICE;
        locationManager = (LocationManager) getSystemService(contenxt);

        /*
         * trying to get the best provider
         * with given conditions
         */
        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        criteria.setAltitudeRequired(false);
        criteria.setCostAllowed(true);
        criteria.setSpeedRequired(false);
        criteria.setBearingRequired(false);
        criteria.setPowerRequirement(Criteria.POWER_LOW);

        String provider = locationManager.getBestProvider(criteria, true);
        Log.d(GAL, provider);

        locationManager.requestLocationUpdates(provider, 3000, 30, locationListener);
        location = locationManager.getLastKnownLocation(provider);

Note: Dont forget to implement Location Listener注意:不要忘记实现位置监听器

Answer to note: You are not including mapView with findViewById(R.id.mapView) its already included in your xml so in your layout.请注意:您没有将 mapView 与 findViewById(R.id.mapView) 包括在您的 xml 中,因此在您的布局中。 You are just getting reference to mapView to use builtin methods like setZoom, animateTo etc.您只是在参考 mapView 以使用 setZoom、animateTo 等内置方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM