【发布时间】:2015-03-09 05:24:03
【问题描述】:
我不明白为什么 locationManager.requestLocationUpdates 无法解析。我搜索了答案,它说您必须导入不再使用的 LocationClient。
这是我如何找到我当前的定位方法。如果我遗漏了什么,请告诉我。
private void setUpMap() {
// Enable MyLocation Layer of Google Map
mMap.setMyLocationEnabled(true);
// Get LocationManager object from System Service LOCATION_SERVICE
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// Create a criteria object to retrieve provider
Criteria criteria = new Criteria();
// Get the name of the best provider
String provider = locationManager.getBestProvider(criteria,true);
LocationListener locationChangeListener = new LocationListener() {
public void onLocationChanged(Location l) {
if (l != null) {
Log.i("SuperMap", "Location changed : Lat: " + l.getLatitude() + " Lng: " +
l.getLongitude());
}
}
public void onProviderEnabled(String p) {
}
public void onProviderDisabled(String p) {
}
public void onStatusChanged(String p, int status, Bundle extras) {
}
};
locationManager.requestLocationUpdates(provider,0,0,locationChangeListener);
// Get Current Location
Location myLocation = locationManager.getLastKnownLocation(provider);
// set map type
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
// Get latitude of the current location
double latitude = myLocation.getLatitude();
// Get longitude of the current location
double longitude = myLocation.getLongitude();
// Create a LatLng object for the current location
LatLng latLng = new LatLng(latitude, longitude);
// Show the current location in Google Map
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
// Zoom in the Google Map
mMap.animateCamera(CameraUpdateFactory.zoomTo(14));
mMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).title("You are here!").snippet("Consider yourself located"));
}
}
这是我的导入
import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import com.google.android.gms.location.LocationListener;
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.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.location.FusedLocationProviderApi;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.GeofencingApi;
【问题讨论】:
标签: android import geolocation location locationmanager