意图使用当前位置启动地图以放置标记

时间:2014-02-04 06:05:16

标签: android google-maps android-intent google-maps-api-3

我目前正在尝试构建一个应用程序,将地图应用程序启动到当前的GPS位置。然后,用户可以在该位置放置标记。我有一个简单Place Marker按钮的布局,可以正确启动地图应用,但我不知道如何让它在用户当前的GPS位置进行区域设置。现在我把它硬编码到某个地址。

Button placeMarkerButton = (Button) findViewById(R.id.bLaunchMaps); 
        placeMarkerButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                try {
                    String address = " 1120 N Street Sacramento, Sacramento CA 94273";
                    Intent maps = new Intent(Intent.ACTION_VIEW, Uri.parse("geo:0,0?q=" + address));
                    startActivity(maps);
                } catch (Exception e) {
                    Log.e(TAG, e.toString());
                }
            }
        });

任何人都可以提供有关我如何实现这一目标的见解吗?一个工作的例子会很棒,因为我还不熟悉Android开发。

2 个答案:

答案 0 :(得分:1)

这是我的GPS课程

public class GPS {

    private static final TAG = "GPS";
    private static boolean pGps, pNetwork;
    private static LocationManager locManager;
    private static String provider;
    private static double longitude;
    private static double latitude;


    private static void updateAvailability(){
        try {
            pNetwork = locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
            provider = LocationManager.NETWORK_PROVIDER;
        } catch (Exception ex) {
            Log.w(TAG,"Ex getting NETWORK provider");
        }
        try {
            pGps = locManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
            provider = LocationManager.GPS_PROVIDER;
        } catch (Exception ex) {
            Log.w(TAG,"Ex getting GPS provider");
        }
    }

    public static Location getLastLocation(Context ctx){
        Location loc = null;
        if(ctx != null){
            if(locManager == null){
                locManager = (LocationManager) ctx.getSystemService(Context.LOCATION_SERVICE);
            }
            updateAvailability();
            if(provider!=null){
                loc = locManager.getLastKnownLocation(provider);
            }
        }
        return loc;
    }       


}

现在,您可以通过按钮显示具有正确纬度和逻辑度的地图。

Button placeMarkerButton = (Button) findViewById(R.id.bLaunchMaps); 
placeMarkerButton.setOnClickListener(new OnClickListener() {     
  @Override
  public void onClick(View v) {
  try {  
  Location loc = GPS.getLastLocation(this.getApplicationContext());
  Double myLongitude = loc.getLongitude();
  Double myLatitude = loc.getLatitude();
  //using google maps you will create a map setting lat & long.     
  String urlAddress = "http://maps.google.com/maps?q="+ myLatitude +"," + myLongitude +"("+ title + ")&iwloc=A&hl=es";     
  //second option:: urlAddress = "http://maps.googleapis.com/maps/api/streetview?size=500x500&location=" + myLatitude + "," + myLongitude + "&fov=90&heading=235&pitch=10&sensor=false";
  //third option:: urlAddress = "geo:<" + myLatitude + ">,<" + myLongitude +">?q=<" + latitude + ">,<" + longitude +">(this is my currently location)"                                
  Intent maps = new Intent(Intent.ACTION_VIEW, Uri.parse(urlAddress)));
  startActivity(maps);
  } catch (Exception e) {
  Log.e(TAG, e.toString());
  }
 }
});

答案 1 :(得分:0)

试试这个:

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("geo:<lat>,<long>?q=<lat>,<long>(Label+Name)"));
startActivity(intent);

如果您不想要标签,可以删除(标签+名称)。

参考:

How to show marker in Maps launched by geo uri Intent?