在谷歌地图中显示当前位置的标记

时间:2016-08-29 10:02:47

标签: android google-maps-markers

我想在Fragment课程中使用标记显示Google地图中的当前位置。我已经显示了地图,但在初始化NullpointerException时,我得到MapFragment

我尝试了这个初始化:

 MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
 mapFragment.getMapAsync(this);

这是onMapReady()函数:

 @Override
public void onMapReady(GoogleMap googleMap) {

    this.googlemap_global = googleMap;
    googlemap_global.setMyLocationEnabled(true);
    googlemap_global.setTrafficEnabled(true);
    googlemap_global.setIndoorEnabled(true);
    googlemap_global.setBuildingsEnabled(true);
    googlemap_global.getUiSettings().setZoomControlsEnabled(true);

}

这是布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context=".GPSTracker2" >


<fragment
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="400dp"
    class="com.google.android.gms.maps.MapFragment" />

<Button
    android:id="@+id/gps_service_start_stop_button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="GPS Service starten"/>

我在这里设置了标记:

LatLng latlng = new LatLng(latitude_global,longitude_global );

    if(googlemap_global != null){
        Marker aktueller_standort = googlemap_global.addMarker(new MarkerOptions().position(latlng).title("Standort"));

        googlemap_global.moveCamera(CameraUpdateFactory.newLatLngZoom(latlng, 15));

        // Zoom in, animating the camera.
        googlemap_global.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
    }

这是logcat输出:

java.lang.NullPointerException: Attempt to invoke virtual method 

'android.app.Fragment android.app.FragmentManager.findFragmentById(int)' on a null object reference
at com.gpstracker.viktor.gpstracker2.Home_Fragment.<init>(Home_Fragment.java:82)
at com.gpstracker.viktor.gpstracker2.GPSTracker2.selectItemFromDrawer(GPSTracker2.java:105)
at com.gpstracker.viktor.gpstracker2.GPSTracker2.access$000(GPSTracker2.java:28)
at com.gpstracker.viktor.gpstracker2.GPSTracker2$1.onItemClick(GPSTracker2.java:67)
at android.widget.AdapterView.performItemClick(AdapterView.java:310)
at android.widget.AbsListView.performItemClick(AbsListView.java:1145)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3066)
at android.widget.AbsListView$3.run(AbsListView.java:3903)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5461)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

感谢您的帮助。

2 个答案:

答案 0 :(得分:2)

您的XML文件

 <FrameLayout
        android:id="@+id/mapContainer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

Java文件代码

 FrameLayout mapContainer = (FrameLayout) findViewById(R.id.mapContainer);
 //show map of 40% of screen
 DisplayMetrics dm = new DisplayMetrics();
 getWindowManager().getDefaultDisplay().getMetrics(dm);
 LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT);
    params.height = (int) ((float) dm.heightPixels * (float) 0.5);
    mapContainer.setLayoutParams(params);
    FragmentManager fm = getFragmentManager();
    MapFragment mapFragment = MapFragment.newInstance();
    fm.beginTransaction().replace(R.id.mapContainer, mapFragment).commit();

 mapFragment.getMapAsync(new OnMapReadyCallback() {
                @Override
                public void onMapReady(GoogleMap googleMap) {
                    GoogleMap  mMap = googleMap;
                    mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
                    addMarks(mMap);

在地图上显示标记

    private void addMarks(GoogleMap googleMap) {
    LatLngBounds.Builder builder = new LatLngBounds.Builder();
    LatLng latLng = new LatLng(lat1, long1);
    builder.include(latLng);
    MarkerOptions markerOptions = new MarkerOptions();
    markerOptions.position(latLng);
    markerOptions.title(currentLocation);
    googleMap.addMarker(markerOptions);

    }

答案 1 :(得分:1)

如下所示更改

<fragment
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="400dp"
    class="com.google.android.gms.maps.MapFragment" />

<fragment
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="400dp"
      class="com.google.android.gms.maps.SupportMapFragment" />

MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);

SupportMapFragment branchdetails_map =
                (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);