我正在使用谷歌地图。
我的代码如下:
public class MainActivity extends AppCompatActivity implements LocationListener {
private GoogleMap map;
private LocationManager mLocationManager;
private Location mLocation;
boolean isGPSEnabled;
boolean isNetworkEnabled;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toast.makeText(MainActivity.this, "Inside OnCreate", Toast.LENGTH_LONG).show();
initializeMap();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
// To Initialize Map..
protected void initializeMap() {
if (map == null) {
map = ((MapFragment) getFragmentManager().findFragmentById(
R.id.map)).getMap();
if (map == null) {
Toast.makeText(MainActivity.this, "Sorry! Map Is Not Created",
Toast.LENGTH_SHORT).show();
}
////Function calling here...
getCurrentLocaion();
}
//For Zoomin, ZoomOut
map.setMyLocationEnabled(true);
// Adding and showing marker while touching the GoogleMap
map.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng arg0) {
// Clears any existing markers from the GoogleMap
map.clear();
// Creating an instance of MarkerOptions to set position
MarkerOptions markerOptions = new MarkerOptions();
// Setting position on the MarkerOptions
markerOptions.position(arg0);
// Animating to the currently touched position
map.animateCamera(CameraUpdateFactory.newLatLng(arg0));
// Adding marker on the GoogleMap
Marker marker = map.addMarker(markerOptions);
// Showing InfoWindow on the GoogleMap
marker.showInfoWindow();
}
});
}
public void getCurrentLocaion() {
// Getting Current Location..
mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
boolean isGPSEnabled = mLocationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
boolean isNetworkEnabled = mLocationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
Toast.makeText(MainActivity.this, "GPS is not Enabled", Toast.LENGTH_LONG).show();
} else {
// First get location from Network Provider
if (isNetworkEnabled) {
mLocationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
5000,
5000, MainActivity.this);
if (mLocationManager != null) {
mLocation = mLocationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (mLocation != null) {
onLocationChanged(mLocation);
}
}
}
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled) {
if (mLocation == null) {
if (checkSelfPermission(android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// public void requestPermissions(@NonNull String[] permissions, int requestCode)
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for Activity#requestPermissions for more details.
return;
}
mLocationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
5000,
5000, this);
if (mLocationManager != null) {
mLocation = mLocationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (mLocation != null) {
//Called here...
onLocationChanged(mLocation);
}
}
}
}
}
}
////Called in getCurrentLocation function...
public void onLocationChanged(Location location) {
map.clear();
MarkerOptions mp = new MarkerOptions();
mp.title("My Postion");
mp.position(new LatLng(location.getLatitude(), location.getLongitude()));
map.addMarker(mp);
map.animateCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(location.getLatitude(), location.getLongitude()), 13));
//TO ADD ZOOMIN-ZOOMOUT button...
map.getUiSettings().setZoomControlsEnabled(true);
}
问题是当我启动应用程序时Marker没有显示在当前位置,当我点击地图时它正在工作。
应该是什么问题?
答案 0 :(得分:1)
试试这个,
private GoogleMap mMap;
mMap.addMarker(new MarkerOptions()
.position(BNE_LOCATION)
.title("Washington Park")
.snippet("425, New Jersey")
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.pin_red)));