为什么地图标记突然倾斜周围的地图 [英] Why Does Map Marker Lurch Around The Map

查看:128
本文介绍了为什么地图标记突然倾斜周围的地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Android的工作室和我有地图碎片(com.google.android.gms.maps.SupportMapFragment),有两个标记。一个是静态的,被称为目标。其他跟踪我的动作,被称为猎人。它的工作原理,但问题是,猎人标记蹒跚有关。我有更新的时间间隔设置为15毫秒,但走高速只更新大约每隔块。

那么,如何正确添加标记以跟踪我的位置,并将它是相当准确和更新的实时?或者我究竟做错了什么?

格雷格

的onCreate()

  setUpMapIfNeeded();

    如果(MMAP!= NULL){
        经纬度目标=新的经纬度(TargetLat,TargetLong);
        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(目标,15));
    }


    LocationManager locationManager =(LocationManager)this.getSystemService(this.LOCATION_SERVICE);

    LocationListener的LocationListener的=新LocationListener的(){
        公共无效onLocationChanged(位置定位){
            makeUseOfNewLocation(位置);
        }

        公共无效onStatusChanged(字符串商,INT地位,捆绑演员){}

        公共无效onProviderEnabled(字符串提供商){
            registerForUpdates();
        }

        公共无效onProviderDisabled(字符串提供商){
            deregisterForUpdates();
        }
    };

    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0,LocationListener的);


私人无效registerForUpdates(){
    _locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,GPS_FREQUENCY_MILLIS,0,_locationListener);
}
 

更新标记时的位置变化。当我们接近目标,振动和更改文本。

 私人无效makeUseOfNewLocation(位置LOC){
    HunterLat = loc.getLatitude();
    HunterLong = loc.getLongitude();

    如果(猎人!= NULL){
        经纬度NewLoc =新的经纬度(HunterLat,HunterLong);
        Hunter.setPosition(NewLoc);

        TextView的txtItem =(TextView中)findViewById(R.id.ItemName);

        如果(HunterLong> TargetLong  -  0.0007&功放;&安培; HunterLong< TargetLong + 0.0007和放大器;&安培; HunterLat> TargetLat  -  0.0007&功放;&安培; HunterLat< TargetLat + 0.0007){
            txtItem.setTextColor(Color.MAGENTA);
            txtItem.setText(了itemname);
            如果(!振捣){
                振捣=真;
                振动器V =(震动)this.getSystemService(Context.VIBRATOR_SERVICE);
                v.vibrate(500);
            }
        }
        其他 {
            txtItem.setTextColor(Color.BLACK);
            txtItem.setText(getResources()的getString(R.string.hunt_item));
        }
    }
    其他 {
        猎人= mMap.addMarker(新MarkerOptions()位置(新的经纬度(HunterLat,HunterLong))标题(您));
        BitmapDesc​​riptor图标2 = BitmapDesc​​riptorFactory.fromResource(R.drawable.ic_map_person);
        Hunter.setIcon(图标2);

    }
}
 

设置地图,并添加静态目标标记

 私人无效setUpMapIfNeeded(){
    //做一个空检查确认,我们还没有实例化的地图。
    如果(MMAP == NULL){
        //尝试获取来自SupportMapFragment地图。
        MMAP =((SupportMapFragment)getSupportFragmentManager()。findFragmentById(R.id.map))
                .getMap();
        //检查如果我们成功取得地图。
        如果(MMAP!= NULL){
            setUpMap();
        }
    }
}

私人无效setUpMap(){
    标记目标= mMap.addMarker(新MarkerOptions()位置(新的经纬度(TargetLat,TargetLong))标题(地点)。);
    BitmapDesc​​riptor图标= BitmapDesc​​riptorFactory.fromResource(R.drawable.ic_target);
    Target.setIcon(图标);
}
 

解决方案

在看到你的<一个href="http://stackoverflow.com/questions/30833628/force-gps-to-come-on?noredirect=1#comment49713022_30833628">other问题,它看起来像无地图活动是获得 onProviderEnabled()回调,但地图的活动是没有得到它(除非GPS无线电已启用在使用地图活动的)。

而不是只调用了 registerForUpdates() onProviderEnabled()回调,调用它的onCreate()如果启用了GPS无线电,以及回退到网络位置,如果GPS被禁用,网络位置已启用。

事情是这样的:

  LocationManager locationManager =(LocationManager)this.getSystemService(this.LOCATION_SERVICE);
    如果(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
        registerForUpdates();
    }
    否则,如果(locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0,LocationListener的);
    }
 

另外要注意的是,你应该注销从的onPause()改写为您的活动,要求位置更新的所有位置的回调,这样你就不必不必要的电池消耗造成你的应用程序。

I'm in Android Studio and I have a map fragment (com.google.android.gms.maps.SupportMapFragment) that has two markers. One is static and called Target. The other tracks my movements and is called Hunter. It works but the problem is that the Hunter marker lurches about. I have the update interval set at 15 milliseconds, but at walking speed it only updates about once every block.

So, how do I properly add a marker to track my location and have it be reasonably accurate and update in real-time? Or what am I doing wrong here?

Greg

onCreate()

    setUpMapIfNeeded();

    if (mMap != null) {
        LatLng Target = new LatLng(TargetLat, TargetLong);
        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(Target, 15));
    }


    LocationManager locationManager = (LocationManager) this.getSystemService(this.LOCATION_SERVICE);

    LocationListener locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
            makeUseOfNewLocation(location);
        }

        public void onStatusChanged(String provider, int status, Bundle extras) {}

        public void onProviderEnabled(String provider) {
            registerForUpdates();
        }

        public void onProviderDisabled(String provider) {
            deregisterForUpdates();
        }
    };

    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);


private void registerForUpdates() {
    _locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, GPS_FREQUENCY_MILLIS, 0, _locationListener);
}

Update marker when location changes. When we get close to the target, vibrate and change the text.

    private void makeUseOfNewLocation(Location loc) {
    HunterLat = loc.getLatitude();
    HunterLong = loc.getLongitude();

    if (Hunter != null) {
        LatLng NewLoc = new LatLng(HunterLat, HunterLong);
        Hunter.setPosition(NewLoc);

        TextView txtItem = (TextView) findViewById(R.id.ItemName);

        if (HunterLong > TargetLong - .0007 && HunterLong < TargetLong + .0007 && HunterLat > TargetLat - .0007 && HunterLat < TargetLat + .0007) {
            txtItem.setTextColor(Color.MAGENTA);
            txtItem.setText(ItemName);
            if (!Vibrated){
                Vibrated = true;
                Vibrator v = (Vibrator) this.getSystemService(Context.VIBRATOR_SERVICE);
                v.vibrate(500);
            }
        }
        else {
            txtItem.setTextColor(Color.BLACK);
            txtItem.setText(getResources().getString(R.string.hunt_item));
        }
    }
    else {
        Hunter = mMap.addMarker(new MarkerOptions().position(new LatLng(HunterLat, HunterLong)).title("You"));
        BitmapDescriptor icon2 = BitmapDescriptorFactory.fromResource(R.drawable.ic_map_person);
        Hunter.setIcon(icon2);

    }
}

Set up the map and add the static target marker

   private void setUpMapIfNeeded() {
    // Do a null check to confirm that we have not already instantiated the map.
    if (mMap == null) {
        // Try to obtain the map from the SupportMapFragment.
        mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                .getMap();
        // Check if we were successful in obtaining the map.
        if (mMap != null) {
            setUpMap();
        }
    }
}

private void setUpMap() {
    Marker Target = mMap.addMarker(new MarkerOptions().position(new LatLng(TargetLat, TargetLong)).title(Location));
    BitmapDescriptor icon = BitmapDescriptorFactory.fromResource(R.drawable.ic_target);
    Target.setIcon(icon);
}

解决方案

After seeing your other question, it looks like the non-map Activity is getting the onProviderEnabled() callback, but your map Activity is not getting it (unless the GPS radio is enabled during use of the map Activity).

Instead of only calling registerForUpdates() in the onProviderEnabled() callback, call it in onCreate() if the GPS radio is enabled, and fall-back to Network Location if GPS is disabled and Network Location is enabled.

Something like this:

    LocationManager locationManager = (LocationManager) this.getSystemService(this.LOCATION_SERVICE);
    if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
        registerForUpdates();
    }
    else if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
    }

Another thing to note is that you should unregister from all location callbacks in the onPause() override for your Activities that request location updates, so that you don't have unnecessary battery drain caused by your app.

这篇关于为什么地图标记突然倾斜周围的地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆