Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
363 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 Answer

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);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...