2017-09-13 78 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);