Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
369 views
in Technique[技术] by (71.8m points)

java - Google Maps: Current Location Marker (Period updates for GMaps)

So I've been able to get periodic updates of my current location through the developer android page, making your app location aware. Now, whenever my location changes, I am able to get the latitude and longitude of that location. However, who do i implement this with Google Maps?

This line below implements a button on my map that finds my current location and places a blue dot/marker on it (does not receive periodic updates)

mMap.setMyLocationEnabled(true);

What should I put in my onLocationChanged() event in order for the blue dot to be updated with the new lat and long?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The blue dot and the precision circle are automatically managed by the map and you can't update it or change it's symbology. In fact, it's managed automatically using it's own LocationProvider so it gets the best location resolution available (you don't need to write code to update it, just enable it using mMap.setMyLocationEnabled(true);).

If you want to mock it's behaviour you can write something like this (you should disable the my location layer doing mMap.setMyLocationEnabled(false);):

private BitmapDescriptor markerDescriptor;
private int accuracyStrokeColor = Color.argb(255, 130, 182, 228);
private int accuracyFillColor = Color.argb(100, 130, 182, 228);

private Marker positionMarker;
private Circle accuracyCircle;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // ...

    markerDescriptor = BitmapDescriptorFactory.fromResource(R.drawable.yourmarkericon);
}

@Override
public void onLocationChanged(Location location) {
    double latitude = location.getLatitude();
    double longitude = location.getLongitude();
    float accuracy = location.getAccuracy();

    if (positionMarker != null) {
        positionMarker.remove();
    }
    final MarkerOptions positionMarkerOptions = new MarkerOptions()
            .position(new LatLng(latitude, longitude))
            .icon(markerDescriptor)
            .anchor(0.5f, 0.5f);
    positionMarker = mMap.addMarker(positionMarkerOptions);

    if (accuracyCircle != null) {
        accuracyCircle.remove();
    }
    final CircleOptions accuracyCircleOptions = new CircleOptions()
            .center(new LatLng(latitude, longitude))
            .radius(accuracy)
            .fillColor(accuracyFillColor)
            .strokeColor(accuracyStrokeColor)
            .strokeWidth(2.0f);
    accuracyCircle = mMap.addCircle(accuracyCircleOptions);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...