这是我的TrackMap.java
public class TrackMap extends FragmentActivity implements LocationListener {
GoogleMap map;
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_track_map);
boolean lowPowerMoreImportantThanAccurancy = true;
LocationRequest request = LocationRequest.create();
request.setPriority(lowPowerMoreImportantThanAccurancy ?
LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY:
LocationRequest.PRIORITY_HIGH_ACCURACY);
map = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map)).getMap();
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
// Enabling MyLocation Layer of Google Map
map.setMyLocationEnabled(true);
Marker marker = map.addMarker(new MarkerOptions()
.position(new LatLng(0, 0)).draggable(true)
// Set Opacity specified as a float between 0.0 and 1.0,
// where 0 is fully transparent and 1 is fully opaque.
.alpha(0.7f).flat(true));
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
map.clear();
MarkerOptions mp = new MarkerOptions();
mp.position(new LatLng(location.getLatitude(), location.getLongitude()));
mp.title("Current Position");
map.addMarker(mp);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
}
我的问题是如何让用户在地图上添加标记,我的TrackMap.java能够获取坐标并保存在变量中。我该怎么办?
从我在网上搜索,他们只是教授已知坐标的标记。但现在我想让用户在不知道坐标的情况下添加标记。
答案 0 :(得分:1)
您可以使用地理编码。地理编码是将街道地址或位置的其他描述转换为(纬度,经度)坐标的过程。一旦获得坐标,就可以在那里添加标记。
答案 1 :(得分:1)
如何在用户触摸地图时添加标记?这是代码:
map.setOnMapClickListener(new OnMapClickListener() {
@Override
public void onMapClick(LatLng point) {
map.addMarker(new MarkerOptions().position(point));
// you can get latitude and longitude also from 'point'
// and using Geocoder, the address
}
});
答案 2 :(得分:0)
这样做可以采取以下几个步骤: -