谷歌地图:在正确加载当前位置之前显示“固定位置”

时间:2016-05-04 09:23:44

标签: android google-maps

我想使用谷歌地图显示我当前的位置,如图2所示。但是,由于谷歌地图需要时间加载我的当前位置,地图首先显示我在代码中设置的位置“LatLng mPosition =新的LatLng(34.6767,33.04455);“。我试图用“LatLng mPosition”替换它但同样的问题仍然存在。有任何纠正措施吗?

GoogleMap googleMap;
GetLocation getLocation = new GetLocation();    

//LatLng mPosition = new LatLng(34.6767, 33.04455);
LatLng mPosition;
final Marker marker_final = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map);
getLocation.getLocation(getApplicationContext(), locationResult);
MapFragment mapFragment = (MapFragment)        getFragmentManager().findFragmentById(R.id.map_fragment);
googleMap=mapFragment.getMap();
}


public LocationResult locationResult = new LocationResult() {
@Override
public void gotLocation(Location location) {
    double Longitude = location.getLongitude();
    double Latitude = location.getLatitude();

    Toast.makeText(getApplicationContext(), "Got Location Long: "+Longitude+"; Lat: "+Latitude,
        Toast.LENGTH_LONG).show();

    mPosition = new LatLng(Latitude, Longitude);

    googleMap.setMyLocationEnabled(true);
    googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(mPosition, 13));
    CameraUpdate zoom = CameraUpdateFactory.zoomTo(18);
    googleMap.animateCamera(zoom);

    /*
    googleMap.addMarker(new MarkerOptions()
    .title("Shop")
    .snippet("Is this the right location?")
    .position(mPosition)).setDraggable(true);
    */

    googleMap.addMarker(setMarkerOptions("Your Shop","The Right Position?",mPosition)).setDraggable(true);

    // map.setInfoWindowAdapter(new PopupAdapter(getLayoutInflater()));
    googleMap.setOnInfoWindowClickListener(GmapDragActivity.this);
    googleMap.setOnMarkerDragListener(GmapDragActivity.this);


}
};

private MarkerOptions setMarkerOptions(String title,String msg,LatLng position){
MarkerOptions options=new MarkerOptions();  

options.title(title);
options.snippet(msg);
options.position(position);

return options;
}

I enter image description here

1 个答案:

答案 0 :(得分:0)

在您的活动中,首次加载地图时,您可以在地图上显示最后的已知位置。对于你的onCreate,添加:

LocationManager mLocationManager = (LocationManager) mContext
            .getSystemService(LOCATION_SERVICE);
if (mLocationManager != null) {
      Location location = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
      LatLng newLatLng = new LatLng(location.getLatitude(), location.getLongitude());
      mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(newLatLng, 10));

      //
      //If you want to add marker
      //
      mMap.addMarker(new MarkerOptions().position(newLatLng));
}

由于您已经在使用GPS,因此您可能已为此代码添加了必要的权限。