【问题标题】:How can I let google maps api v2 go directly to my location如何让 google maps api v2 直接进入我的位置
【发布时间】:2013-01-04 16:27:18
【问题描述】:

我是真正的 Google Maps API 菜鸟,因此我们非常感谢您的帮助。我在这里想看到的是,当我打开我的应用程序时,相机需要直接移动到我当前的位置并放置蓝点。我该如何做到这一点?

我制作了一个示例代码,以便每个人都可以理解它并在需要时实施到他们的代码中:

GoogleMap map = ((SupportMapFragment)  getSupportFragmentManager().findFragmentById(R.id.general_map)).getMap();
map.setMapType(GoogleMap.MAP_TYPE_HYBRID);

if( helper.isGPSEnabled() ){
    map.move... // move directly to my current position
}

请帮忙...

【问题讨论】:

  • 我在哪里得到 lat 和 lng。您粘贴的评论根本没有帮助。

标签: android camera move google-maps-android-api-2


【解决方案1】:

这就是我能够开始工作的地方。它显示您当前的位置并在那里放置一个标记。这适用于 Google Maps API v2

private void setUpMap() {
    // Enable MyLocation Layer of Google Map
    googleMap.setMyLocationEnabled(true);

    // Get LocationManager object from System Service LOCATION_SERVICE
    LocationManager locationManager = (LocationManager) getSystemService(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
    googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);

    // 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        
    googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));

    // Zoom in the Google Map
    googleMap.animateCamera(CameraUpdateFactory.zoomTo(20));
    googleMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).title("You are here!"));
}

【讨论】:

  • 关于位置 myLocation = locationManager.getLastKnownLocation(provider); myLocation 为空,知道吗?
  • 它不工作!!我得到的只是:找不到 Google Play 服务资源。检查您的项目配置以确保包含资源。
【解决方案2】:

您需要首先确定用户的位置。之后,您在地图上设置纬度和经度。另外不要忘记设置缩放级别以放大地图。

判断gps是否开启:

How do I find out if the GPS of an Android device is enabled

Android - Is there a way to listen if GPS was enabled or disabled

有关位置策略的更多信息:

http://developer.android.com/guide/topics/location/strategies.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-06
    • 2013-02-15
    • 1970-01-01
    • 2012-11-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多