2017-09-13 80 views
0

我跟隨了24小時教科書並創建了一個Android應用程序,該應用程序應該啓動Google地圖並根據硬編碼到應用程序中的緯度和經度座標導航到特定位置。但是,當我啓動應用程序並運行代碼時,它會打開Goog​​le地圖或Google地球,並始終默認使用我當前的位置。我如何阻止它做到這一點?使用Java覆蓋Android手機上的默認位置

我試圖讓我的應用去特定的經度和緯度,但是當它啓動Google地圖或Google地球時,它總是默認爲我當前的位置。 Java的代碼如下:

Button mapButton = (Button) findViewById(R.id.button2); 
mapButton.setOnClickListener(new View.OnClickListener() { 
    @override 
    public void onClick(View view) { 
    String geoURI = "geo:37.422,-122.0847z=23"; 
    Uri geo = Uri.parse(geoURI); 
    Intent mapIntent = new Intent(Intent.ACTION_VIEW, geo); 
    if (mapIntent.resolveActivity(getPackageManager()) != null) { 
     startActivity(mapIntent); 

我如何得到它覆蓋缺省位置,往我已經硬編碼以上的座標?

+2

https://開頭計算器.com/questions/3990110/how-to-show-marker-in-maps-launching-by-geo-uri-intent – neethu

回答

1

Looking at the documentation,行:

String geoURI = "geo:37.422,-122.0847z=23"; 

似乎無效語法,但可能會想放大。嘗試:但是

String geoURI = "geo:37.422,-122.0847?z=23"; 

縮放級別只到21日,所以也嘗試:

String geoURI = "geo:37.422,-122.0847?z=21"; 

如果沒有工作,利用基本的字符串沒有任何變焦:

String geoURI = "geo:37.422,-122.0847"; 
+0

謝謝。是的,這是一個語法錯誤。添加「?」並將縮放級別更改爲z = 21解決了問題 – Pickles57

0

嘗試這個:

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

你可以省略(Label +姓名),如果你不想要標籤,它會根據最近的街道或其他認爲相關的東西隨機選擇一個。

0

您可以創建LatLng對象並告訴谷歌地圖加載位置。代碼做,這將是這樣的:

double lat=26.52719; 
double lng=88.72448; 
LatLng myLocation = new LatLng(lat,lng); 
       if(this.googleMap !=null){ 
        this.googleMap.addMarker(new MarkerOptions().position(myLocation).title("Your current Location").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED))); 
        this.googleMap.moveCamera(CameraUpdateFactory.newLatLng(myLocation)); 
       } 

記住,你應該在onMapReady回調將此並關閉在谷歌地圖您的當前位置擺放,你可以做

this.googleMap.setMyLocationEnabled(true);