我编写了一个使用谷歌地图API的简单程序,该程序首先关注用户的位置。 应用程序正常运行,但在函数
中mMap.animateCamera (CameraUpdateFactory.newLatLngZoom (getMyLocation (), 22));
我将缩放值设置得太大(例如22),网络摄像头地图突出显示距离用户位置大约两个街区的点。 如果你能找到解决方案,我会很高兴的。 以下是我的计划:
package ebookfrenzy.com.mapdemo;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v4.app.FragmentActivity;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.UiSettings;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MapDemoActivity extends FragmentActivity {
private GoogleMap mMap; // Might be null if Google Play services APK is not available.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map_demo);
//the message of the location service
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Make sure your location services are turned on, whether not do you want turn them on?").setPositiveButton("Yes", dialogClickListener)
.setNegativeButton("No", dialogClickListener).show();
setUpMapIfNeeded();
}
//Setting pop-up window and is derivatives
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
switch (which){
case DialogInterface.BUTTON_POSITIVE:
//Yes button clicked
Intent intent=new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
break;
case DialogInterface.BUTTON_NEGATIVE:
//No button clicked
break;
}
}
};
@Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
}
/**
* Sets up the map if it is possible to do so (i.e., the Google Play services APK is correctly
* installed) and the map has not already been instantiated.. This will ensure that we only ever
* call {@link #setUpMap()} once when {@link #mMap} is not null.
* <p>
* If it isn't installed {@link SupportMapFragment} (and
* {@link com.google.android.gms.maps.MapView MapView}) will show a prompt for the user to
* install/update the Google Play services APK on their device.
* <p>
* A user can return to this FragmentActivity after following the prompt and correctly
* installing/updating/enabling the Google Play services. Since the FragmentActivity may not
* have been completely destroyed during this process (it is likely that it would only be
* stopped or paused), {@link #onCreate(Bundle)} may not be called again so we should call this
* method in {@link #onResume()} to guarantee that it will be called.
*/
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
setUpMap();
//Setting up the map settings
UiSettings mapSettings;
mapSettings = mMap.getUiSettings();
//Approval to the application to use location services
mMap.setMyLocationEnabled(true);
//lock all options built-in buttons in google maps without locking their abilities
mapSettings.setMyLocationButtonEnabled(false);
mapSettings.setZoomControlsEnabled(false);
mapSettings.setCompassEnabled(false);
/***********************************************************************************************************
LatLng MUSEUM = new LatLng(38.8874245, -77.0200729);
Marker museum = mMap.addMarker(new MarkerOptions().position(MUSEUM).title("Museum").snippet("National Air and Space Museum"));
***********************************************************************************************************/
}
}
}
/**
* This is where we can add markers or lines, add listeners or move the camera. In this case, we
* just add a marker near Africa.
* <p>
* This should only be called once and when we are sure that {@link #mMap} is not null.
*/
private void setUpMap() {
mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(getMyLocation(), 22));
}
public LatLng getMyLocation()
{
//get locationManager object from System Service LOCATION_SERVICE
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, true);
Location myLocation = locationManager.getLastKnownLocation(provider);
double latitude = myLocation.getLatitude();
double longitude = myLocation.getLongitude();
LatLng latLng = new LatLng(latitude, longitude);
return latLng;
}
}