谷歌地图标记路由

时间:2015-02-17 04:25:54

标签: android google-maps routes marker transit

我是谷歌地图API V2中的新手,我想制作一个标记/方向指针,指向谷歌地图上的路线。如果您不理解我的意思,请访问此链接https://developers.google.com/maps/documentation/android/,然后点击平面标记。那里的代码甚至没有完整,我不知道该怎么做。

请帮帮我!非常感谢

1 个答案:

答案 0 :(得分:1)

fyi,https://developers.google.com/maps/documentation/android/start#getting_the_google_maps_android_api_v2

yourmap.xml

<FrameLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="2dp"
    >
    <com.google.android.gms.maps.MapView
         android:id="@+id/mapview"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
    />
</FrameLayout>

youractivity.class

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    Log.i(TAG, "oncreate");
    View rootView = inflater.inflate(R.layout.yourmap, container, false);

     // Gets the MapView from the XML layout and creates it
    mapView = (MapView) rootView.findViewById(R.id.mapview);
    mapView.onCreate(savedInstanceState);
    setCurrentLocationMarker();

    return rootView;
}

public void setCurrentLocationMarker(){
    try {
    // Gets to GoogleMap from the MapView and does initialization stuff
        map = mapView.getMap();
        map.getUiSettings().setMyLocationButtonEnabled(false);
        map.setMyLocationEnabled(true);

        // Needs to call MapsInitializer before doing any CameraUpdateFactory calls
        try {
            MapsInitializer.initialize(getActivity());
        } catch (Exception e) {
            Log.e(TAG , "Exception: "+e.getMessage());
            e.printStackTrace();
        }

        gps = new GPSTracker(getActivity());

        if(gps.canGetLocation()) {

            myLatitude = gps.getLatitude();
            myLongitude = gps.getLongitude();

            if(myLatitude==0 && myLongitude==0){
                LocationManager lm = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE); 
                Location location = getLastKnownLocation(lm);
                Log.i(TAG, "location : "+location);
                if(location!=null){
                     myLatitude = location.getLatitude();
                     myLongitude = location.getLongitude();
                }
            }

        } else {
            // Can't get location.
            // GPS or network is not enabled.
            // Ask user to enable GPS/network in settings.
            gps.showSettingsAlert();
            Log.i(TAG, "gps.canGetLocation() : "+gps.canGetLocation());
            return;
        }

        Log.i(TAG, "myLatitude  :"+myLatitude+", myLongitude: "+myLongitude);


        // Updates the location and zoom of the MapView
        if(myLatitude>0 && myLongitude>0){
            CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(myLatitude, myLongitude), 10);
            map.animateCamera(cameraUpdate);

            MarkerInfo markerinfo = new MarkerInfo("", "Current Location", myLatitude, myLongitude, true);
            addMarker(markerinfo);
        }


        map.setOnMarkerClickListener(onClickListener);
        map.setOnInfoWindowClickListener(onInfoWindowClickListener);            
        marker.showInfoWindow();

    } catch (Exception e) {
       Log.e(TAG, "Exception : "+e.getMessage());
    }
}