gpt4 book ai didi

Android 谷歌地图位置管理器无法在 SupportMapFragment 中工作

转载 作者:行者123 更新时间:2023-11-30 03:14:55 25 4
gpt4 key购买 nike

我目前正在尝试将谷歌地图包含到我的应用程序中。主要目的是在加载 map 后自动将相机移动到具有缩放功能的设备当前位置。

为此,我需要一个位置管理器。在下面的代码中,我试图实现一个,但没有成功。我还尝试从 fragment 类传递上下文。

但我仍然从 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!"));
}

最佳答案

getSystemService()Context 的方法,不是Fragment 的方法。你可能想这样做:

LocationManager locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);

或者这个:

LocationManager locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);

关于Android 谷歌地图位置管理器无法在 SupportMapFragment 中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20291600/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com