我正在通过一个应用程序来获取当前位置使用gps并将其转换为相应的地址。现在我想在谷歌地图中标记这个位置。我可以显示地图但不能标记位置。这是我的代码
package com.gpsshoppe;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.Menu;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.maps.GeoPoint;
public class MapActivity extends FragmentActivity {
GoogleMap map;
//Location location;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new GpsMapLocationActivity();
mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
if (map == null) {
map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
map.setMyLocationEnabled(true);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.map, menu);
return true;
}
private class GpsMapLocationActivity implements LocationListener{
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
if (location != null) {
final GeoPoint point = new GeoPoint(
(int) (location.getLatitude() * 1E6),
(int) (location.getLongitude() * 1E6));
String address = ConvertPointToLocation(point);
address.toString();
Log.i("ADRESSS", ""+point);
}
}
public String ConvertPointToLocation(GeoPoint point) {
// TODO Auto-generated method stub
String address = "";
Geocoder geoCoder=new Geocoder(getBaseContext(), Locale.getDefault());
try {
List<Address> addresses = geoCoder.getFromLocation(
point.getLatitudeE6() / 1E6,
point.getLongitudeE6() / 1E6, 1);
if (addresses.size() > 0) {
for (int index = 0;
index < addresses.get(0).getMaxAddressLineIndex(); index++)
address += addresses.get(0).getAddressLine(index) + " ";
}
}
catch (IOException e) {
e.printStackTrace();
}
return address;
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status,
Bundle extras) {
// TODO Auto-generated method stub
}
}
}
请帮帮我... 提前谢谢..
答案 0 :(得分:2)
只需添加一个包含当前位置的标记:
private GoogleMap mMap;
mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
mMap.addMarker(new MarkerOptions()
.position(new LatLng(yourlat, yourlong))
.title("My position"));
所有关于标记,infowindow等..
https://developers.google.com/maps/documentation/android/marker#add_a_marker
答案 1 :(得分:1)
Google Maps API V2不支持 GeoPoint 对象,而是必须使用 LatLng 对象位置表示创建标记。
而不是:
if (location != null) {
final GeoPoint point = new GeoPoint(
(int) (location.getLatitude() * 1E6),
(int) (location.getLongitude() * 1E6));
String address = ConvertPointToLocation(point);
address.toString();
Log.i("ADRESSS", ""+point);
要在此位置添加标记,请执行以下操作:
if (location != null) {
final GeoPoint point = new GeoPoint(
(int) (location.getLatitude() * 1E6),
(int) (location.getLongitude() * 1E6));
String address = ConvertPointToLocation(point);address.toString();
Log.i("ADRESSS", ""+point);
Marker marker = map.addMarker(new MarkerOptions()
.position(new LatLng(location.getLatitude(), ocation.getLongitude()))
.title(name)
.snippet(address)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.your_marker_icon)));