Android 获取用户上的标记';谷歌地图上的当前位置
我正在使用谷歌地图。我想让marker默认指向用户的当前位置。我该怎么做。这是我的代码,有没有办法只在谷歌地图上获取印度地图。请帮助我Android 获取用户上的标记';谷歌地图上的当前位置,android,google-maps,Android,Google Maps,我正在使用谷歌地图。我想让marker默认指向用户的当前位置。我该怎么做。这是我的代码,有没有办法只在谷歌地图上获取印度地图。请帮助我 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_map_activity1); MapFragment fm = (MapFragment)
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);
}
});
}
要使标记指向当前位置,可以执行以下操作:
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);
谢谢你的回答我编辑了我的问题…它向我显示了错误…我也发布了日志…请帮助我…你添加了地图吗。setMyLocationEnabled(true);在地图实例化之后?是的,我已经添加了它..并且它正在工作..非常感谢您…但是当我添加了CameraUpdate之后它就不工作了…并且标记也没有设置在当前位置上…它应该可以工作,检查您的代码也许您有什么错误!