【问题标题】:Android google maps location manager not working in SupportMapFragmentAndroid 谷歌地图位置管理器在 SupportMapFragment 中不起作用
【发布时间】: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


    【解决方案1】:

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

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

    或者这个:

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

    【讨论】:

    • 感谢您的快速回复。当我使用您的代码时,出现以下异常: StackOverFlowError
    • 好吧,你需要找出一个合适的上下文来使用,它不会导致递归调用。也许当您调用getActivity() 时,它会再次调用onCreateView(),这会导致您的代码再次调用getActivity(),以此类推。也许您应该在以后的某个方法中调用您的setUpMap()(例如onActivityCreated()onStart())。
    猜你喜欢
    • 2015-07-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-26
    • 1970-01-01
    • 2012-02-24
    • 1970-01-01
    • 2017-02-24
    • 1970-01-01
    相关资源
    最近更新 更多