【发布时间】:2013-11-29 18:30:55
【问题描述】:
我目前正在尝试将谷歌地图包含到我的应用程序中。主要目的是在加载地图后通过缩放自动将相机移动到设备的当前位置。
为此,我需要一个位置管理员。在下面的代码中,我试图实现一个,但没有成功。我还尝试从片段类传递上下文。
但我仍然从 eclipse 中收到这条消息:方法 getSystemService(String) 未定义导航类型
任何想法我做错了什么?
public class Navigation extends SupportMapFragment {
private GoogleMap map;
private Context mContext;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View v = inflater.inflate(R.layout.activity_navigation, null, false);
getContextFromConfig();
setUpMapIfNeeded();
return v;
}
private void getContextFromConfig()
{
ContextConfig config = new ContextConfig();
mContext = config.getContext();
}
@Override
public void onResume() {
super.onResume();
setUpMapIfNeeded();
}
private void setUpMapIfNeeded() {
if (map == null) {
map = ((SupportMapFragment) getFragmentManager().findFragmentById(
R.id.map)).getMap();
} else {
System.out.println("Google Maps is not available.");
}
if (map != null) {
setUpMap();
}
}
private void setUpMap() {
map.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker").snippet("snippet"));
// Enable MyLocation Layer of Google Map
map.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);
// Get Current Location
Location myLocation = locationManager.getLastKnownLocation(provider);
//set map type
map.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
map.moveCamera(CameraUpdateFactory.newLatLng(latLng));
// Zoom in the Google Map
map.animateCamera(CameraUpdateFactory.zoomTo(14));
map.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).title("You are here!"));
}
}
【问题讨论】:
标签: android google-maps