2012-05-03 238 views
0

我已经在这一个多星期了。我试过在我的应用中设置Google地图活动,但没有运气。我也看到你可以从你的应用程序调用Google地图应用程序。通过使用Onclicklistener。如何使用按钮从我的应用程序打开Goog​​le地图应用程序?这是我使用的代码...谷歌地图使用按钮/ Onclicklistener打开谷歌地图应用

import com.google.android.maps.GeoPoint; 
import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.net.Uri; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 

public class Store10 extends Activity { 

    Context context; 
    GeoPoint geo; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.store10); 

     Button num10 = (Button) findViewById(R.id.pNumber10); 
     Button nav10 = (Button) findViewById(R.id.Map10); 

     num10.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View view) { 
       // TODO Auto-generated method stub 
       Intent myIntent = new Intent(view.getContext() ,Num10.class); 
       startActivityForResult(myIntent, 0); 
      } 
     }); 
     nav10.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View view) { 
       // TODO Auto-generated method stub 
       String uri = String.format("geo:%f,%f",40,325874,76,002211); 
       Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri)); 
       context.startActivity(intent); 
      } 
     }); 
    } 

} 

这里的Onclicklistener我试图建立

nav10.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View view) { 
       // TODO Auto-generated method stub 
       String uri = String.format("geo:%f,%f",40,325874,76,002211); 
       Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri)); 
       context.startActivity(intent); 
      } 
     }); 
+0

什么是对的OnClick错误??? –

回答

4

你可以试试这些代码行...

String uri = "http://maps.google.com/maps?saddr=" + "9982878"+","+"76285774"+"&daddr="+"9992084"+","+"76286455"; 
      Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(uri)); 
      intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity"); 
      startActivity(intent); 
+0

现在,而不是硬编码的经度和纬度,你可以把你.. –

+0

我认为这应该为我工作。希望它在地方放置一个标记。因为我有一些地方的我在购物中心。如果你知道在loc上放置一个标记的方法,那么让我知道... – KodiakBear211

2

// ---查看地图按钮---

b3 = (Button) findViewById(R.id.btn_showMap); 
b3.setOnClickListener(new OnClickListener() 
{ 
public void onClick(View arg0){ 
Intent i = new 
Intent(android.content.Intent.ACTION_VIEW, 
Uri.parse("geo:37.827500,-122.481670")); 
startActivity(i); 
} 
}); 
+0

谢谢你的工作。但我有一个问题。当它进入地图。当它在卫星设置关闭,你必须缩小一次才能看到的位置。你可以设置缩放吗?另一个可以使用这种方法设置一个标记。或者你必须在你的应用程序中创建一个地图来做这样的事情? – KodiakBear211

+1

Upvote并接受帖子,如果有用 –

1

请检查here这个网址对于你可能有助于..

Uri uri = Uri.parse("geo:13.070984,80.253639"); 
Intent in = new Intent(Intent.ACTION_VIEW, uri); 
startActivity(in); 
+0

我只是得到一个黑色的屏幕 – KodiakBear211

3

更改URI来设置缩放级别

uri = Uri.parse("geo:37.827500,-122.481670"?z=10"); 

尝试使用此方法添加标记:

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

可以省略(标签+姓名),如果你不想要一个标签,它会选择一个随机基于最近的街道或其认为相关的其他事物。

+0

这是我如何插入它。 – KodiakBear211

+0

Uri.parse(“geo:<40.325874>,<-76.002211>?q = <40.325874>,<-76.002211>(Label + Store10)”));我无法加载uri – KodiakBear211

+0

好吧,是不能加载这个网址... – KodiakBear211

3

这是我做到了......

Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(
"geo:" + marker.getPosition().latitude + 
","  + marker.getPosition().longitude + 
"?q="  + marker.getPosition().latitude + 
","  + marker.getPosition().longitude +         
"("  + marker.getTitle() + ")")); 

intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity"); 
startActivity(intent);