我正在编写一个Android应用程序,我正在尝试进行反向地理编码以显示当前用户在地图上的位置(通过检索用户的纬度和经度)。
我根本没有获得用户的位置,但代码的其他部分正在运行,例如添加标记。
这是我的主要代码:
public class MapsActivity extends AppCompatActivity implements OnMapReadyCallback,
GoogleMap.OnMyLocationChangeListener,
android.location.LocationListener {
private GoogleMap mGoogleMap;
private Geocoder mGeocoder;
private GoogleApiClient mGoogleApiClient = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
SupportMapFragment mSupportMapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mSupportMapFragment.getMapAsync(this);
mGeocoder = new Geocoder(this, Locale.getDefault());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main1, menu);
return true;
}
@Override
protected void onPause() {
super.onPause();
stopLocationUpdates();
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
}
protected void stopLocationUpdates() {
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, (LocationListener) this);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.normal:
mGoogleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
break;
case R.id.satellite:
mGoogleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
break;
case R.id.terrain:
mGoogleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
break;
case R.id.hybrid:
mGoogleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
break;
case R.id.none:
mGoogleMap.setMapType(GoogleMap.MAP_TYPE_NONE);
break;
}
return true;
}
@Override
public void onMapReady(GoogleMap googleMap) {
mGoogleMap = googleMap;
mGoogleMap.getUiSettings().setZoomControlsEnabled(true);
mGoogleMap.getUiSettings().setCompassEnabled(true);
mGoogleMap.getUiSettings().setMyLocationButtonEnabled(true);
mGoogleMap.setMyLocationEnabled(true);
mGoogleMap.setOnMyLocationChangeListener(this);
showMyLocationOnScreen();
}
@Override
public void onMyLocationChange(Location location) {
double currentLatitude = location.getLatitude();
double currentLongitude = location.getLongitude();
LatLng latLng = new LatLng(currentLatitude, currentLongitude);
MarkerOptions options = new MarkerOptions()
.position(latLng)
.title("I am here!");
mGoogleMap.addMarker(options);
mGoogleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
showMyLocationOnScreen();
}
private void showMyLocationOnScreen() {
final LatLng PES_UNIVERSITY=new LatLng(12.9338,77.5345);
mGoogleMap.addMarker(new MarkerOptions()
.position(PES_UNIVERSITY)
.title("PES UNIVERSITY")
.snippet("Banashankari,3rd stage,Bangalore")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA)));
final LatLng PES_GYM = new LatLng(12.933996, 77.53438);
mGoogleMap.addMarker(new MarkerOptions()
.position(PES_GYM)
.title("PES GYM")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA)));
final LatLng PES_CricketGround = new LatLng(12.93443, 77.533819);
mGoogleMap.addMarker(new MarkerOptions()
.position(PES_CricketGround)
.title("PES CRICKET GROUND")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));
final LatLng NesCafe = new LatLng(12.934913, 77.534608);
mGoogleMap.addMarker(new MarkerOptions()
.position(NesCafe)
.title("NESCAFE")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE)));
final LatLng Chemistry_Lab = new LatLng(12.934801, 77.534702);
mGoogleMap.addMarker(new MarkerOptions()
.position(Chemistry_Lab)
.title("CHEMISTRY LAB")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_VIOLET)));
final LatLng CISCO = new LatLng(12.935496, 77.534723);
mGoogleMap.addMarker(new MarkerOptions()
.position(CISCO)
.title("CISCO")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_CYAN)));
final LatLng CampusMart = new LatLng(12.935682, 77.534463);
mGoogleMap.addMarker(new MarkerOptions()
.position(CampusMart)
.title("CAMPUS MART")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)));
final LatLng CafeCoffeeDay = new LatLng(12.933591, 77.534636);
mGoogleMap.addMarker(new MarkerOptions()
.position(CafeCoffeeDay)
.title("CAFE COFFEE DAY")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE)));
final LatLng BasketballCourt = new LatLng(12.933727, 77.534896);
mGoogleMap.addMarker(new MarkerOptions()
.position(BasketballCourt)
.title("BASKETBALL COURT")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
final LatLng Pir_Cube = new LatLng(12.933696, 77.534601);
mGoogleMap.addMarker(new MarkerOptions()
.position(Pir_Cube)
.title("PIR CUBE")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ROSE)));
final LatLng Food_Court = new LatLng(12.933618, 77.534333);
mGoogleMap.addMarker(new MarkerOptions()
.position(Food_Court)
.title("FOOD COURT")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE)));
final LatLng PES_Boys_Hostel = new LatLng(12.933276, 77.534935);
mGoogleMap.addMarker(new MarkerOptions()
.position(PES_Boys_Hostel)
.title("PES BOYS HOSTEL")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW)));
final LatLng IT_Block = new LatLng(12.933278, 77.534423);
mGoogleMap.addMarker(new MarkerOptions()
.position(IT_Block)
.title("IT BLOCK")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
final LatLng South_Canteen = new LatLng(12.934559, 77.535666);
mGoogleMap.addMarker(new MarkerOptions()
.position(South_Canteen)
.title("SOUTH CANTEEN")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE)));
final LatLng MRD = new LatLng(12.9354, 77.534942);
mGoogleMap.addMarker(new MarkerOptions()
.position(MRD)
.title("MRD BLOCK")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
final LatLng Panini_Block = new LatLng(12.932731, 77.534548);
mGoogleMap.addMarker(new MarkerOptions()
.position(Panini_Block)
.title("PANINI BLOCK")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
final LatLng South_Mess = new LatLng(12.93321, 77.534903);
mGoogleMap.addMarker(new MarkerOptions()
.position(South_Mess)
.title("SOUTH MESS")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE)));
final LatLng B_Block = new LatLng(12.935343, 77.534306);
mGoogleMap.addMarker(new MarkerOptions()
.position(B_Block)
.title("B BLOCK")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
}
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
}
我该如何解决这个问题?