使用多段线选项,地图更改地图上的毛刺 [英] Location change glitches on map using polyline options

查看:92
本文介绍了使用多段线选项,地图更改地图上的毛刺的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序面临一些问题,特别是在我的谷歌地图上绘制路线图。我已经在我的房子周围制作了测试路线,并发现,GPS提供商不如runtastic或endomondo等类似应用程序的准确性。


有时,位置监听器会在我的地图上做出不可理解的变化,即使有完美的GPS信号,折线也会在我的位置附近的地图上画出任何线条。


其他时间,它不起作用。


任何人都可以解释我(例如Im 5)其他健身应用程序如何获得其当前位置和剧情路线到谷歌地图上?谢谢!



//地图片段

  map =((MapFragment )getFragmentManager()。findFragmentById(R.id.fragment1))。getMap(); 
map.setMyLocationEnabled(true);
locationManager =(LocationManager)getSystemService(LOCATION_SERVICE);

Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria,true);
$ b $ //权限增益

if(ActivityCompat.checkSelfPermission(this,android.Manifest.permission.ACCESS_FINE_LOCATION)!= PackageManager.PERMISSION_GRANTED&&& ActivityCompat.checkSelfPermission(this ,android.Manifest.permission.ACCESS_COARSE_LOCATION)!= PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(this,new String [] {android.Manifest.permission.ACCESS_COARSE_LOCATION},
MY_PERMISSION_ACCESS_COURSE_LOCATION);
return;
}
isLocationEnabled(getApplicationContext());

位置myLocation = locationManager.getLastKnownLocation(provider);
if(myLocation!= null){
latitude = myLocation.getLatitude();
longitude = myLocation.getLongitude();
float zoom =(float)17.0;
map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude,longitude),zoom));
Log.e(TAG,GPS is on);
Toast.makeText(MainActivity.this,latitude:+ latitude +longitude:+ longitude,Toast.LENGTH_SHORT).show();
} else {
locationManager.requestLocationUpdates(provider,5000,0,this);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,5000,10,this);


$ b $ public void onLocationChanged(Location mylocation){

if(lastLocationloc == null){
lastLocationloc = mylocation;
}
LatLng lastLatLng = locationToLatLng(lastLocationloc);
LatLng thisLatLng = locationToLatLng(mylocation);
map.addPolyline(new PolylineOptions()。add(lastLatLng).add(thisLatLng).width(6).color(Color.RED));
lastLocationloc = mylocation;
Toast.makeText(MainActivity.this,!!!! Location CHANGE !!!!,Toast.LENGTH_SHORT).show();



public static LatLng locationToLatLng(Location loc){
if(loc!= null)
返回新的LatLng(loc.getLatitude() ,loc.getLongitude());
返回null;
}




例如,我想像这样描绘它:





但我的应用程序工作自己的方式..

解决方案

首先,您需要考虑收到的位置的准确性。您可以使用 Location.getAccuracy()方法获得准确性(文档)。精确度以米为单位,所以越低越好:

  if(location.getAccuracy()< MINIMUM_ACCURACY){
//将新位置添加到您的多段线
}

您可以设置您的例如 MINIMUM_ACCURACY 为10米。

另一方面,您可能需要添加一个新的位置只有当您的新位置远离您最后一次添加位置的给定距离时,您的多段线才会生效。举个例子:

  private static final float MINIMUM_ACCURACY = 10; 
private static final float MINIMUM_DISTANCE_BETWEEN_POINTS = 20;
私人位置lastLocationloc;

// ...

public void onLocationChanged(Location mylocation){
if(mylocation.getAccuracy()< MINIMUM_ACCURACY){
if (lastLocationloc == null || lastLocationloc.distanceTo(mylocation)> MINIMUM_DISTANCE_BETWEEN_POINTS){
//将新位置添加到您的多段线

lastLocationloc = mylocation;
}
}
}


Im facing some problem with my application, specifically with route plot/draw on my google maps. Ive made test route around my house and found out, that GPS providers are not as accurate as similar applications like runtastic or endomondo.

Sometimes Location listener makes incomprehensible changes on my map and the polyline then draws any lines on the map near my location even with perfect GPS signal.
Some other time, it just doesnot work. It does not listen to location change.

Can anybody explain me (like Im five) how does other fitness application get their current position and the plot route onto the google map? Thanks!

//Map Fragment

    map = ((MapFragment) getFragmentManager().findFragmentById(R.id.fragment1)).getMap();
    map.setMyLocationEnabled(true);
    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

    Criteria criteria = new Criteria();
    String provider = locationManager.getBestProvider(criteria, true);

//Permission gain

    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_COARSE_LOCATION},
                MY_PERMISSION_ACCESS_COURSE_LOCATION);
        return;
    }
    isLocationEnabled(getApplicationContext());

    Location myLocation = locationManager.getLastKnownLocation(provider);
    if (myLocation != null) {
        latitude = myLocation.getLatitude();
        longitude = myLocation.getLongitude();
        float zoom = (float) 17.0;
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude, longitude), zoom));
        Log.e("TAG", "GPS is on");
        Toast.makeText(MainActivity.this, "latitude:" + latitude + " longitude:" + longitude, Toast.LENGTH_SHORT).show();
    } else {
        locationManager.requestLocationUpdates(provider, 5000, 0, this);
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 5000, 10, this);

    }

 public void onLocationChanged(Location mylocation) {

    if (lastLocationloc == null) {
        lastLocationloc = mylocation;
    }
    LatLng lastLatLng = locationToLatLng(lastLocationloc);
    LatLng thisLatLng = locationToLatLng(mylocation);
    map.addPolyline(new PolylineOptions().add(lastLatLng).add(thisLatLng).width(6).color(Color.RED));
    lastLocationloc = mylocation;
    Toast.makeText(MainActivity.this, "!!!!Location CHANGE!!!!", Toast.LENGTH_SHORT).show();

}

public static LatLng locationToLatLng(Location loc) {
    if (loc != null)
        return new LatLng(loc.getLatitude(), loc.getLongitude());
    return null;
}


For example, I want to plot it like this:


But my application works its own way..

解决方案

First of all, you need to take into account the accuracy of the received location. You can get the accuracy using the Location.getAccuracy() method (documentation). The accuracy is measured in meters, so the lower the better:

if (location.getAccuracy() < MINIMUM_ACCURACY) {
    // Add the new location to your polyline
}

You can set your MINIMUM_ACCURACY to be 10 meter for example.

On the other hand, you may want to add a new location to your polyline only if your new location is farther than a given distance to your last added location. As an example:

private static final float MINIMUM_ACCURACY = 10;
private static final float MINIMUM_DISTANCE_BETWEEN_POINTS = 20;
private Location lastLocationloc;

// ...

public void onLocationChanged(Location mylocation) {
    if (mylocation.getAccuracy() < MINIMUM_ACCURACY) {
        if (lastLocationloc == null || lastLocationloc.distanceTo(mylocation) > MINIMUM_DISTANCE_BETWEEN_POINTS) {
            // Add the new location to your polyline

            lastLocationloc = mylocation;
        }
    }
}

这篇关于使用多段线选项,地图更改地图上的毛刺的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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