【问题标题】:Google Maps Android API (Latest Dec 8, 2014 Release) Centralizing Around User's LocationGoogle Maps Android API(2014 年 12 月 8 日最新发布)围绕用户位置进行集中处理
【发布时间】:2015-03-08 12:30:59
【问题描述】:

我正在开发一些 POC(概念证明)应用程序,以帮助我构建更大的应用程序。 我会直截了当。我只是想以用户当前位置为中心打开以地图为主要活动的应用程序。

当我尝试移动相机时,由于 NullPointerException,我的代码下方出现错误 - 因为 mLastLocation 为空。 同样,每当我的应用程序打开时,我希望它以用户的位置为中心。 我正在关注 Google Maps Android API v2 的最新版本(2014 年 12 月 8 日)。

我不想创建 GoogleMap 成员变量。

非常感谢任何建议。提前感谢帮助解决我的问题的人。

public class MapActivity extends Activity implements
    OnMapReadyCallback, ConnectionCallbacks, OnConnectionFailedListener {

protected static final String TAG = "Application";
protected GoogleApiClient mGoogleApiClient;
protected Location mLastLocation;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_map);
    buildGoogleApiClient();
    MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}

@Override
public void onMapReady(GoogleMap map) {
    map.setMyLocationEnabled(true);

    map.moveCamera(CameraUpdateFactory.newLatLngZoom(
            new LatLng(mLastLocation.getLatitude(), mLastLocation.getLongitude()), 13)); //<-- NullPointerException

}

@Override
public void onConnected(Bundle connectionHint) {
    mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
}

【问题讨论】:

    标签: android google-maps android-studio google-maps-android-api-2


    【解决方案1】:

    我认为从 FragmentActivity 而不是 MainActivity 扩展它会解决您的问题。

    public class MainActivity extends FragmentActivity
        implements OnMapReadyCallback, ConnectionCallbacks, OnConnectionFailedListener {
    ...
    }
    

    对于 XML 布局类:

    <?xml version="1.0" encoding="utf-8"?>
    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        android:name="com.google.android.gms.maps.MapFragment"
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
    

    【讨论】:

      【解决方案2】:

      以下是我在应用程序中使用 Google 地图的示例:

      在布局/map.xml 中:

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

      在我的 MapFragment.java 中:

      public class MapFragment extends Fragment implements GoogleMap.OnInfoWindowClickListener {
      
      private MapView mapView;
      private GoogleMap map;
      
      public MapFragment() {
          // Required empty public constructor
      }
      
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
          View v = inflater.inflate(R.layout.map, container, false);
      
          // Gets the MapView from the XML layout and creates it
          mapView = (MapView) v.findViewById(R.id.location_map);
          mapView.onCreate(savedInstanceState);
      
          // Gets to GoogleMap from the MapView and does initialization stuff
          map = mapView.getMap(); 
          if (map == null)
          {
              return v;
          }
          map.getUiSettings().setMyLocationButtonEnabled(false); 
          map.setMyLocationEnabled(true);
          if (map == null) {
              Toast.makeText(this.getActivity(), "Google Maps not available",  Toast.LENGTH_LONG).show();
              return v;
          }
      
          MapsInitializer.initialize(this.getActivity());
      
      
          LatLng location =  new LatLng(51.5033630,-0.1276250);
          map.setOnInfoWindowClickListener(this);
      
           Marker market =  map.addMarker(new MarkerOptions()
                  .position(location)
                  .title("My marker")
                  .snippet("desc");
      
          // Updates the location and zoom of the MapView
          CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(ProfileManager.getLatitude(),
                  ProfileManager.getLongitude()), 13);
          map.animateCamera(cameraUpdate);
      return v;
      }
      
      
      @Override
      public void onResume() {
          mapView.onResume();
          super.onResume();
      }
      
      @Override
      public void onDestroy() {
          super.onDestroy();
          mapView.onDestroy();
      }
      
      @Override
      public void onLowMemory() {
          super.onLowMemory();
          mapView.onLowMemory();
      }
      }
      

      希望对你有帮助!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-12-11
        • 1970-01-01
        • 1970-01-01
        • 2012-06-13
        • 2013-08-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多