2012-09-01 39 views
0

我從用戶輸入的是經度和緯度的兩個值,並且有一個顯示位置按鈕,單擊它時會使用這兩個經度和緯度值顯示位置。 我只需要使用上showlocation()的意圖或按鈕Android Intent for google.maps onclick

請檢查我的代碼

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 

    android:orientation="horizontal" > 


<EditText android:id="@+id/lat" 
     android:layout_weight="50" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:hint="@string/lat" /> 

<EditText 
    android:id="@+id/longi" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="50" 
    android:ems="10" 
    android:hint="@string/longi" > 

    <requestFocus /> 
</EditText> 

<Button 
    android:layout_width="1dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="100" 
    android:text="@string/button_send" 
    android:onClick="showlocation()" /> 
</LinearLayout> 



public void showlocation(){ 

     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

不是很清楚你想要什麼。

嘗試設置onClick屬性上的按鈕showLocation而不是showLocation()

後,你需要在你的Activity

public void showLocation(View v) { 
    // do what you want if the button is clicked by user 

} 

創建public方法,也可以刪除您buttononClick屬性,爲該按鈕添加id屬性,以便您可以在您的Activity上找到它,並讓您的Activity implements onClickListener

Button yourbutton = (Button) findViewById(R.id.ID_YOU_DEFINE); 
yourbutton.setOnClickListener(this); 

// You have to override this method since your activity implements it. 
@Override 
public void onClick(View v) { 
    switch (v.getId()) { 
    case R.id.ID_YOU_DEFINE : 
     // do what you want if button with id ID_YOU_DEFINE is clicked 

    case R.id.OTHER_ID : 
     // do what you want 

    } 

} 
+0

讓我說清楚。 我從用戶的2個輸入,這將是經度和緯度。 如果他們點擊按鈕,它會顯示地圖或maps.google.com顯示他們的位置。我是新手,所以不太瞭解。 將不勝感激 –