【问题标题】:GoogleMaps API v2 - Tracking only via GPSGoogle Maps API v2 - 仅通过 GPS 进行跟踪
【发布时间】:2015-05-22 15:56:49
【问题描述】:

我想解决问题,但我不知道如何解决。我只需要通过 GPS 跟踪我的应用程序位置,无需通过网络和互联网进行跟踪。现在,当我在外面时,我有错误的位置,以非常大的分歧来找我。需要以 100 米的精度定位。

这是我的活动

public class MainActivity extends AppCompatActivity implements OnMapReadyCallback {

private boolean aFoundMyLocation;

private Document aDocument;
private GoogleMap aGoogleMap;
private GoogleDirection aGoogleDirection;

private LatLng aCurrentLocation;
...

@InjectView(R.id.txt_distance)
TextView txtDistance;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ButterKnife.inject(this);

    // initial map center settings
    aTargetLocation = new LatLng(49.199362, 18.737382);
    aGoogleDirection = new GoogleDirection(this);
    aFoundMyLocation = false;

    // inflate mapFragment
    SupportMapFragment mapFragment = (SupportMapFragment)
            getSupportFragmentManager().findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}

...

@Override
public void onMapReady(GoogleMap googleMap) {
    aGoogleMap = googleMap;
    aGoogleMap.setBuildingsEnabled(true);
    aGoogleMap.setMyLocationEnabled(true);

    aGoogleMap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() {
        @Override
        public void onMyLocationChange(Location location) {
            aCurrentLocation = new LatLng(location.getLatitude(), location.getLongitude());
            ...
            }
        }
    });
}
}

和我的清单的一部分

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

<application
...

    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="..." />

</application>

有人可以给我一个建议,如何编辑它以仅通过 GPS 以 100 米的精度跟踪实际位置?谢谢

【问题讨论】:

    标签: android gps google-maps-api-2


    【解决方案1】:
    protected LocationManager locationManager;
    
    //use this on your onCreate Method
    
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
    

    【讨论】:

    • 在 requestLocationUpdates 上,最后一个参数上下文监听器无法解析,知道我必须改变什么吗?
    • "this" 指的是应用的 locationlistener。确保你有这个:import android.location.LocationListener
    • 好的,谢谢,错误的导入,我来自 google.android.gms.maps ...我会去外面试试
    • 你的 MainActivity 也应该实现 LocationListener,例如 public class MainActivity extends FragmentActivity implements LocationListener
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多