如何调整 MapFragment (Android)

How to tweak MapFragment (Android)

我制作了一个基本上是 GoogleMaps 的 MapFragment。通过 fragmentManager 和片段事务在 activity 中调用此片段。我现在想调整我的地图以添加一些标记和动态缩放。我现在有点挣扎。也许你们中的一些人可以看看并提供帮助。

MapsActivity.java

public class MapsActivity extends Activity {

Context context;
Button showMap;
Button showMyLocation;

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

    context = this;

    showMap = (Button)findViewById(R.id.showMap);
    showMap.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            FragmentManager fm = getFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();
            MapFragment mf = new MapFragment();
            MapFragment.googleMap.addMarker(new MarkerOptions().position(new LatLng(Double.parseDouble("2.900000"), Double.parseDouble("1.55555")))
            .title("oh yeah"));
            ft.add(R.id.maps_layout, mf);
            ft.commit();
        }
    });

    showMyLocation = (Button)findViewById(R.id.showMyLocation);
    showMyLocation.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });
}

MapFragment.java

public class MapFragment extends Fragment {

public static GoogleMap googleMap;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.map_fragment_layout, container, false);
    return v;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    googleMap = ((com.google.android.gms.maps.MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();
}


 }

更新到上面的代码后,它在堆栈跟踪中给我这个错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.model.Marker com.google.android.gms.maps.GoogleMap.addMarker(com.google.android.gms.maps.model.MarkerOptions)' on a null object reference
        at com.example.dell.exampleapplication.MapsActivity.onClick(MapsActivity.java:42)
        at android.view.View.performClick(View.java:5156)
        at android.view.View$PerformClick.run(View.java:20755)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:145)
        at android.app.ActivityThread.main(ActivityThread.java:5834)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)

这可能会提供额外的帮助信息。 谢谢你的帮助。

好的,这是我的使用方法:

public class Mapfrag2 extends Fragment{
    public static   GoogleMap map;
    

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.map_fragment_layout, container, false);}

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    
        map = ((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();
      
        if (savedInstanceState == null) {
            map.setMyLocationEnabled(true);
            CameraUpdate center = CameraUpdateFactory.newLatLng(new LatLng(30.76793169992044, 1.98180484771729));//moving the map to defined position
            CameraUpdate zoom = CameraUpdateFactory.zoomTo(5);
            map.moveCamera(center);
            markerPoints = new ArrayList<LatLng>();

            map.animateCamera(zoom);
            map.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
                @Override
                public void onInfoWindowClick(Marker marker) {
                   //doing something when pressing marker's bubble
                }
            });
        }

    }

   
    
    

   

   

}

在你的 activity 中调用

 MapFrag2.map 

例子

添加来自 activity

的标记
Mapfrag2.map.addMarker(new MarkerOptions().position(
                        new LatLng(Double.parseDouble(lat), Double.parseDouble(lon)))
                        .title(title).snippet(snippet));

正在清除地图

 Mapfrag2.map.clear();

编辑 2

尝试更改这部分代码

public void onClick(View v) {
            FragmentManager fm = getFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();
            MapFragment mf = new MapFragment();
            MapFragment.googleMap.addMarker(new MarkerOptions().position(new LatLng(Double.parseDouble("2.900000"), Double.parseDouble("1.55555")))
            .title("oh yeah"));//move this to the end
            ft.add(R.id.maps_layout, mf);
            ft.commit();
           //here you add your marker
        }

我找到了一个非常简单的解决方案。我只是按照此维基页面 (http://www.techotopia.com/index.php/Working_with_the_Google_Maps_Android_API_in_Android_Studio) 中的步骤操作。它基本上向你解释了一切。

简单提示:让您的 MapActivity 扩展 FragmentActivity!