谷歌地图安卓API(最新2014年12月8日发布)集中用户的位置



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

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

我不想创建谷歌地图成员变量。

任何建议将不胜感激。提前感谢帮助解决我问题的人。

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);
}
我认为

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"/>

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

在布局/地图中.xml :

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

在我的地图片段中.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();
}
}

希望对您有所帮助!

相关内容

最新更新