Android Studio Map Activity app显示当前位置

时间:2015-11-24 09:40:18

标签: android-activity android-studio location maps

我有这段代码。

public class MapsActivity extends FragmentActivity {

    private GoogleMap mMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        setUpMapIfNeeded();
    }

    @Override
    protected void onResume() {
        super.onResume();
        setUpMapIfNeeded();
    }

    private void setUpMapIfNeeded() {

        if (mMap == null) {

            mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                    .getMap();

            if (mMap != null) {

                setUpMap();
            }
        }
    }

    private void setUpMap() {
        mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
        mMap.setMyLocationEnabled(true);
    }

}

它看起来不错,但是当我运行我的应用程序,并试图按下应该显示我当前位置的按钮时,没有任何事情可以有人帮我解决这个问题?请!!!。

3 个答案:

答案 0 :(得分:0)

首先,您需要从谷歌开发者网站获取谷歌地图api密钥,用你的谷歌帐户登录https://developers.google.com/并找到谷歌api,然后添加地图api并获得你自己的api密钥(其自由 ) 那么你应该从新菜单(在Android工作室应用程序中)添加一个新的谷歌地图活动,这会自动构建一个活动并添加google_maps_api.xml,并将权限和元数据添加到您的清单文件

在清单xml文件中使用google map api密钥

        <meta-data  android:name="com.google.android.maps.v2.API_KEY"
        android:value="your_google_maps_key" />

以及google_maps_api.xml文件

<resources>

<string name="google_maps_key" translatable="false"    templateMergeStrategy="preserve">
    your_google_maps_key
</string>
</resources>

现在您可以在应用程序上使用谷歌地图了

现在您需要使用此方法设置地图:

要查找您的位置,您需要将位置的坐标保存到位置变量

    private void setUpMap() {

       if (mMap == null) {
        // Try to obtain the map from the SupportMapFragment.
         mMap = ((SupportMapFragment)    getSupportFragmentManager().findFragmentById(R.id.map))
                .getMap();
         // Check if we were successful in obtaining the map.
         if (mMap != null) {
            LocationManager lManager; 
            lManager = (LocationManager) getSystemService(LOCATION_SERVICE); 
            Criteria cr = new Criteria(); 
            // find best provider for location services
            String provider = lManager.getBestProvider(cr, true);
            // get your last khown location 
            Location location = lManager.getLastKnownLocation(provider);
            // set update freq for location updating 
            lManager.requestLocationUpdates(provider, 20000, 0, this); 
            // save your location to LatLng variable
            LatLng currentPosition = new LatLng(location.getLatitude(),             location.getLongitude());
            // zoom to your location - 12 is zoom size
            mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(currentPosition,  12));
            // add your custom marker to map
            mMap.addMarker(new       MarkerOptions().position(currentPosition).title("\u200e" + "Me")).setIcon(BitmapDescriptorFactory.fromBitmap(bitmap));
         }
        }
     }

然后在你的活动onCreate

上调用setUpMap()

答案 1 :(得分:0)

按下按钮运行以下代码:

 Location location = map.getMyLocation();
    LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
        CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLng(latLng);
        map.animateCamera(cameraUpdate);

答案 2 :(得分:0)

您也可以执行以下代码:

xml布局中添加 mapview

<com.google.android.gms.maps.MapView
    android:id="@+id/mapview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

然后在你的代码中(我在这里使用 Fragmnet

public class LocationFragment extends Fragment implements OnMapReadyCallback {

  MapView gMapView;
  GoogleMap gMap = null;

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.location_layout, container, false);

    gMapView = (MapView) view.findViewById(R.id.mapview);
    gMapView.getMapAsync(this);  //this is the main line which will make a callback when map is ready

    return view;
  }

  @Override
  public void onMapReady(GoogleMap map) {
    gMap = map;
    gMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new
            LatLng(your_latlng), 12));
  }
}