【问题标题】:How to navigate of a Map (Android Maps API v2)如何导航地图(Android Maps API v2)
【发布时间】:2013-09-03 14:23:04
【问题描述】:

我刚刚让我的地图在我的应用程序中运行。我有问题。当这个地方出现在地图上时。我希望能够单击该地点并将其显示在适当的 Google 地图应用程序中。因此,如果需要,您可以导航到该地点。

所以我有一张地图,上面有一个标记。我想点击标记,然后在谷歌地图中找到地址。这样一来,如果人们要导航,他们只需点击它即可获得路线。

java代码如下:

public class showroommap extends Activity {
static final LatLng HAMBURG = new LatLng(53.558, 9.927);
static final LatLng KIEL = new LatLng(52.633011,-1.132913);
private GoogleMap map;



 @Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
setContentView(R.layout.showroommap);
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
    .getMap();
Marker hamburg = map.addMarker(new MarkerOptions().position(HAMBURG)
    .title("Hamburg"));
Marker kiel = map.addMarker(new MarkerOptions()
    .position(KIEL)
    .title("Kiel")
    .snippet("Kiel is cool")
    .icon(BitmapDescriptorFactory
        .fromResource(R.drawable.ic_launcher)));

//Move the camera instantly to hamburg with a zoom of 15.
map.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));

//Zoom in, animating the camera.
map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
 }

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
 }

} 

这可能吗?

更多信息: 我想点击我的应用程序中显示的标记。单击标记后,它将转到 Google 地图并根据需要定向到该标记。

【问题讨论】:

  • 你应该删除javascript标签,因为这个问题与javascript无关。

标签: android google-maps google-maps-api-2


【解决方案1】:

如果我正确理解您想要的是向 Google 地图应用发送带有纬度和经度的意图,以便用户可以导航到特定位置。以下是我在我的应用中实现它的方式:

Intent navigation = new Intent(Intent.ACTION_VIEW, Uri.parse("google.navigation:q=" +latlong.latitude+","+latlong.longitude));
        startActivity(navigation);

latlong 是 LatLng 类型。

更新 1 - 收听标记上的点击

public class showroommap extends Activity implements onMarkerClickListener {
static final LatLng HAMBURG = new LatLng(53.558, 9.927);
static final LatLng KIEL = new LatLng(52.633011,-1.132913);
private GoogleMap map;



 @Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
setContentView(R.layout.showroommap);
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
    .getMap();
map.setOnMakerClickListener(this); //Register this Activity to the onMarkerClickListener
Marker hamburg = map.addMarker(new MarkerOptions().position(HAMBURG)
    .title("Hamburg"));
Marker kiel = map.addMarker(new MarkerOptions()
    .position(KIEL)
    .title("Kiel")
    .snippet("Kiel is cool")
    .icon(BitmapDescriptorFactory
        .fromResource(R.drawable.ic_launcher)));

//Move the camera instantly to hamburg with a zoom of 15.
map.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));

//Zoom in, animating the camera.
map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
 }

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
 }

@Override
 public boolean onMarkerClick(final Marker marker) {

    if (marker.equals(kiel)) 
    {
        //handle click here
     return true;
    }
    else if (marker.equals(hamburg)) 
    {
        //handle click here
        return true;
    }
    return false;
}

} 

【讨论】:

  • 嗨,是的,我想在我的应用程序中实际调出地图。但是当我点击标记时希望它转到地图并显示方向
  • 是的,所以你要做的是设置 GoogleMap.setOnMarkerClickListener();然后在 onMarkerClick(Marker marker) 上设置并启动 Intent。您可以在 onMarkerClick(Marker marker) 中引用特定标记,方法是使用 if 语句并将传递给方法的标记的标题与您给标记的名称标题相匹配,或者只需测试标记是否是相同的标记以 marker.equals(kiel) 为例。
  • 您好,非常感谢您的回复...是否有这样的例子让我感到困惑
  • 这是我发现的另一篇文章,其中包含一个实现stackoverflow.com/questions/14226453/…。我将编辑我的答案以帮助您。
  • 如果解决了您的问题,请将此问题标记为已回答。
【解决方案2】:

这里也许你可以使用这样的东西

public class showroommap extends Activity implements OnMarkerClickListener{
static final LatLng HAMBURG = new LatLng(53.558, 9.927);
static final LatLng KIEL = new LatLng(52.633011,-1.132913);
private GoogleMap map;



 @Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
setContentView(R.layout.showroommap);
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
    .getMap();
Marker hamburg = map.addMarker(new MarkerOptions().position(HAMBURG)
    .title("Hamburg"));
Marker kiel = map.addMarker(new MarkerOptions()
    .position(KIEL)
    .title("Kiel")
    .snippet("Kiel is cool")
    .icon(BitmapDescriptorFactory
        .fromResource(R.drawable.ic_launcher)));
map.setOnMarkerClickListener(this); 
//Move the camera instantly to hamburg with a zoom of 15.
map.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));

//Zoom in, animating the camera.
map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
 }

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
 }

} 

 @Override
public boolean onMarkerClick(Marker arg0) {
final Context mContext = this;
final LatLng now = arg0.getPosition();
AlertDialog.Builder course = new AlertDialog.Builder(mContext);
    course.setNegativeButton("On Foot", new OnClickListener(){


        @Override
        public void onClick(DialogInterface arg0, int arg1) {
            // TODO Auto-generated method stub
            Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(String.format("google.navigation:ll=%s,%s%s", now.latitude, now.longitude, "&mode=w")));
            mContext.startActivity(i);
        } 

    });

    course.setNeutralButton("By Car", new OnClickListener(){

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
            Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(String.format("google.navigation:ll=%s,%s%s",  now.latitude, now.longitude, "&mode=d")));
            mContext.startActivity(i);
        }

    });
    course.setPositiveButton("On Bike", new OnClickListener(){

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
            Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(String.format("google.navigation:ll=%s,%s%s",  now.latitude, now.longitude, "&mode=b")));
            mContext.startActivity(i);
        }

    });
course.show();
return false;
}

【讨论】:

  • 他希望能够点击标记并让标记启动 Google 地图应用程序...
  • 不是谷歌地图,谷歌导航器,它只是用于标记点击的警告对话框,我想把它取下来,他说这不是你想要的
  • 你去吧,我在我的回答中编辑了你的问题作为一个工作示例
  • public boolean onMarkerClick(Marker arg0) { 错误说它必须是布尔值。如果我将其更改为布尔值,则表示它必须是无效的。所以它盘旋
  • 或者说添加退货声明
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多