将标记设置为圆心(当前位置)

时间:2015-02-16 07:45:41

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

我在我的应用中使用谷歌地图Android API v2,我有一个小问题,如下图所示,但不知道如何解决它。我将标记设置为设备的当前位置,但它现在位于圆的中心。我想将标记设置为圆圈的中心。有可能吗?

感谢您的帮助!

我使用的代码

// Showing status
if(status!= ConnectionResult.SUCCESS){ // Google Play Services are not available

    int requestCode = 10;
    Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, requestCode);
    dialog.show();

}else { // Google Play Services are available

    // Getting reference to the SupportMapFragment of activity_location.xml
    SupportMapFragment mSupportMapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);

    // Getting GoogleMap object from the fragment
    mGoogleMap = mSupportMapFragment.getMap();

    // Enabling MyLocation Layer of Google Map
    mGoogleMap.setMyLocationEnabled(true);

    // Getting LocationManager object from System Service LOCATION_SERVICE
    LocationManager mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

    // Creating a criteria object to retrieve provider
    Criteria criteria = new Criteria();

    // Getting the name of the best provider
    String provider = mLocationManager.getBestProvider(criteria, true);

    // Getting Current Location
    Location location = mLocationManager.getLastKnownLocation(provider);

    if(location!=null){
        onLocationChanged(location);
    }
    mLocationManager.requestLocationUpdates(provider, 20000, 0, this);

    // Getting latitude of the current location
    double latitude = location.getLatitude();

    // Getting longitude of the current location
    double longitude = location.getLongitude();

    // Creating a LatLng object for the current location
    LatLng mLatLng = new LatLng(latitude, longitude);

    //Add Marker to current location of devise
    mGoogleMap.addMarker(new MarkerOptions()
            .position(mLatLng)
            .title("Geolocation system")
            .snippet("Your last current location which was available!"));
    //      .icon(BitmapDescriptorFactory.fromResource(R.drawable.icon_location)));

    // Showing the current location in Google Map
    mGoogleMap.moveCamera(CameraUpdateFactory.newLatLng(mLatLng));

    // Show Zoom buttons
    mGoogleMap.getUiSettings().setZoomControlsEnabled(true);

    // Turns on 3D buildings
    mGoogleMap.setBuildingsEnabled(true);

    // Zoom in the Google Map
    mGoogleMap.animateCamera(CameraUpdateFactory.zoomTo(14));

enter image description here

1 个答案:

答案 0 :(得分:0)

您正在使用GPS和网络获取您的位置。这些没有给出正确的位置,而且您正在使用: -

mGoogleMap.setMyLocationEnabled(真);

这会显示您使用谷歌地图API的位置。所以你和谷歌的联系是不同的。所以圈子和标记显示不同的位置。

因此您可以使用Google的Fuesed Location客户端进行此操作

访问此处获取更多帮助。 https://developer.android.com/reference/com/google/android/gms/location/FusedLocationProviderApi.html