在谷歌地图上获取用户当前位置的标记

时间:2014-11-18 17:32:40

标签: android google-maps

我正在使用谷歌地图。我希望默认情况下标记指向用户的当前位置。我怎么能这样做。这是我的代码,有没有办法只在google map.plz上获取印度地图帮助我..

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_map_activity1);
    MapFragment fm = (MapFragment) getFragmentManager()
            .findFragmentById(R.id.fragment1);

    // Getting Map for the SupportMapFragment
    final GoogleMap map = fm.getMap();
    if(map!=null){
         map.setMyLocationEnabled(true);
        }


        if(map!=null){
            Location myLocation = map.getMyLocation();

            if(myLocation !=null){
            LatLng myLatLng = new LatLng(myLocation.getLatitude(),
                    myLocation.getLongitude());


            CameraPosition myPosition = new CameraPosition.Builder()
                    .target(myLatLng).zoom(17).bearing(90).tilt(30).build();
            map.animateCamera(CameraUpdateFactory.newCameraPosition(myPosition));
           }
        }
    // Setting a click event handler for the map
    map.setOnMapClickListener(new OnMapClickListener() {

        @Override
        public void onMapClick(LatLng latLng) {

            // Creating a marker
            MarkerOptions markerOptions = new MarkerOptions();

            // Setting the position for the marker
            markerOptions.position(latLng);

            // Setting the title for the marker.
            // This will be displayed on taping the marker
            markerOptions.title(latLng.latitude + " : " + latLng.longitude);

            // Clears the previously touched position
            map.clear();

            // Animating to the touched position
            map.animateCamera(CameraUpdateFactory.newLatLng(latLng));

            // Placing a marker on the touched position
            map.addMarker(markerOptions);
            double lat=latLng.latitude;
            double longitude=latLng.longitude;
            String LAT=String.valueOf(lat);
            String LONGITUDE=String.valueOf(longitude);
            Intent in = new Intent(getApplicationContext(),
                    LocationActivity.class);
            // Sending lat/long to next activity
            in.putExtra(TAG_LAT, LAT);
            in.putExtra(TAG_LONG, LONGITUDE);
            startActivityForResult(in, 100);

        }
    });
}

1 个答案:

答案 0 :(得分:0)

要使标记指向当前位置,您可以这样做:

    Location myLcation = map.getMyLocation();

    LatLng myPosition = new LatLng(myLcation.getLatitude(),myLcation.getLongitude());

    MarkerOptions currentLocationMarker = new MarkerOptions()
                .position(myPosition)
                .title(/*YOUR TITLE*/)
                .icon(BitmapDescriptorFactory.fromResource(R.drawable./*YOUR ICON*/));

    map.addMarker(currentLocationMarker);

如果你想放大这个位置:

   CameraUpdate location = CameraUpdateFactory.newLatLngZoom(myPosition, 6);
   mMap.animateCamera(location);
相关问题