0

我的申请有问题。一切都很好,但是当我需要在新的谷歌地图中添加两个标记时是不可能的,因为 mMap = (mMapFragment).getMap() 返回 null (我已经看到了 Log.i)我已经有另一个在第一个活动中映射显示所有最近的标记,它工作得很好。

这是我的 DrawPathActivity

public class DrawPathActivity extends FragmentActivity  {
    private GoogleMap mMap;
    private GoogleMapOptions options = new GoogleMapOptions();
    private MapFragment mMapFragment;

    private PolylineOptions PolyOpt = new PolylineOptions();
    private Polyline polyline;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        mMapFragment = MapFragment.newInstance();
        setContentView(R.layout.activity_draw_path);
        options.mapType(GoogleMap.MAP_TYPE_NORMAL)
        .compassEnabled(true)
        .scrollGesturesEnabled(true)
        .zoomGesturesEnabled(true);
        MapFragment.newInstance(options);
        mMap = ((SupportMapFragment)  getSupportFragmentManager().findFragmentById(R.id.map1)).getMap();
        android.app.FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
        fragmentTransaction.add(R.id.map1, mMapFragment);
        fragmentTransaction.commit();

        mMap.setMyLocationEnabled(true);
        Intent intent = getIntent();
        LatLng cpPos = new LatLng(intent.getExtras().getDouble("cp_lat"), intent.getExtras().getDouble("cp_lon"));
        LatLng poiPos = new LatLng(intent.getExtras().getDouble("POI_lat"), intent.getExtras().getDouble("POI_lon"));
        addMarkers(intent);

        PolyOpt.add(cpPos)
        .add(poiPos)
        .width(10)
        .color(Color.BLUE)
        .geodesic(true);
        polyline = mMap.addPolyline(PolyOpt);

        String url = makeURL(cpPos.latitude, cpPos.longitude, poiPos.latitude, poiPos.longitude);
        drawPath(url, true);

    }



    private void addMarkers(Intent intent) {
        Log.i("mMap ini", String.valueOf(mMap));
        Log.i("mMapFragment", String.valueOf(mMapFragment));
        LatLng cpPos = new LatLng(intent.getExtras().getDouble("cp_lat"), intent.getExtras().getDouble("cp_lon"));
        LatLng poiPos = new LatLng(intent.getExtras().getDouble("POI_lat"), intent.getExtras().getDouble("POI_lon"));
        mMap = (mMapFragment).getMap();  <<<---- mMap IS NULL!!!  

        Log.i("mMap", String.valueOf(mMap));
        mMap.addMarker(new MarkerOptions()
        .position(cpPos)
        .icon(BitmapDescriptorFactory.fromResource(R.drawable.youarehere)));
        mMap.addMarker(new MarkerOptions()
        .position(poiPos)
        .title(intent.getExtras().getString("POI_name"))
        .icon(BitmapDescriptorFactory.fromResource(R.drawable.poi_serena)));        
    }



    public static LatLng locationToLatLng(Location loc) {
        if(loc != null)
            return new LatLng(loc.getLatitude(), loc.getLongitude());
        return null;
    }

    public String makeURL (double sourcelat, double sourcelog, double destlat, double destlog ){
        StringBuilder urlString = new StringBuilder();
        urlString.append("http://maps.googleapis.com/maps/api/directions/json");
        urlString.append("?origin=");// from
        urlString.append(Double.toString(sourcelat));
        urlString.append(",");
        urlString
        .append(Double.toString( sourcelog));
        urlString.append("&destination=");// to
        urlString
        .append(Double.toString( destlat));
        urlString.append(",");
        urlString.append(Double.toString( destlog));
        urlString.append("&sensor=false&mode=driving&alternatives=true");
        return urlString.toString();
    }







    public void drawPath(String  result, boolean withSteps) {

        try {
            //Tranform the string into a json object
            final JSONObject json = new JSONObject(result);
            JSONArray routeArray = json.getJSONArray("routes");
            JSONObject routes = routeArray.getJSONObject(0);
            JSONObject overviewPolylines = routes.getJSONObject("overview_polyline");
            String encodedString = overviewPolylines.getString("points");
            List<LatLng> list = decodePoly(encodedString);

            for(int z = 0; z<list.size()-1;z++){
                LatLng src= list.get(z);
                LatLng dest= list.get(z+1);
                Polyline line = mMap.addPolyline(new PolylineOptions()
                .add(new LatLng(src.latitude, src.longitude), new LatLng(dest.latitude, dest.longitude))
                .width(10)
                .color(Color.BLUE).geodesic(true));
            }
            if(withSteps)
            {
                JSONArray arrayLegs = routes.getJSONArray("legs");
                JSONObject legs = arrayLegs.getJSONObject(0);
                JSONArray stepsArray = legs.getJSONArray("steps");
                //put initial point

                for(int i=0;i<stepsArray.length();i++)
                {
                    Step step = new Step(stepsArray.getJSONObject(i));
                    mMap.addMarker(new MarkerOptions()
                    .position(step.location)
                    .title(step.distance)
                    .snippet(step.instructions)
                    .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));

                }
            }

        } 
        catch (JSONException e) {

        }
    }

这是这个类的xml布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <CheckBox
            android:id="@+id/satellite"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/satellite" />
    </LinearLayout>

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

</LinearLayout>

有人可以帮助我吗?并且可以解释地图是如何为空的?因为我无法在地图上添加两个Marker,我无法完成我的工作。

谢谢大家。

4

1 回答 1

0

当您MapFragment在 XML 中定义类似这样的内容时,无需执行任何FragmentTransaction. 带有地图的片段就在那里。

所以在你的onCreate()方法中尝试改变线条

mMapFragment = MapFragment.newInstance();
setContentView(R.layout.activity_draw_path);
options.mapType(GoogleMap.MAP_TYPE_NORMAL)
.compassEnabled(true)
.scrollGesturesEnabled(true)
.zoomGesturesEnabled(true);
MapFragment.newInstance(options);
mMap = ((SupportMapFragment)  getSupportFragmentManager().findFragmentById(R.id.map1)).getMap();
android.app.FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.map1, mMapFragment);
fragmentTransaction.commit();

setContentView(R.layout.activity_draw_path);
mMap = ((SupportMapFragment)  getSupportFragmentManager().findFragmentById(R.id.map1)).getMap();
// This is how you set up the map options in this case.
// Btw, you could maybe check if you don't set something that
// the map has set by default. I'd be surprised if scrollGestures
// or zoomGestures were disabled by default. Also the setting
// MAP_TYPE_NORMAL is I think useless as it should be the default.
mMap.getUiSettings().setCompassEnabled(true).setScrollGesturesEnabled(true).setZoomGesturesEnabled(true);

然后在该addMarkers()方法中,您可以删除该mMap = (mMapFragment).getMap();行。您已经在onCreate()方法中获得了对地图的引用。

应该工作,让我知道。

于 2013-04-29T09:47:50.383 回答