2017-04-18 89 views
0

我想将此代码集成到我的代码中,但不知道如何使其工作。使用标记在我附近生成随机位置

我得到了返回语句的问题,然后如何用随机生成的位置创建一个标记?
你能告诉我如何让getRandomLocation()方法创建标记?

public Location getRandomLocation() { 
     Location location = new Location(""); 
     LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude()); 
    int radius = 10; 


        double x0 = latLng.latitude; 
      double y0 = latLng.longitude; 

      Random random = new Random(); 

      // Convert radius from meters to degrees 
      double radiusInDegrees = radius/111000f; 

      double u = random.nextDouble(); 
      double v = random.nextDouble(); 
      double w = radiusInDegrees * Math.sqrt(u); 
      double t = 2 * Math.PI * v; 
      double x = w * Math.cos(t); 
      double y = w * Math.sin(t); 

      // Adjust the x-coordinate for the shrinking of the east-west distances 
      double new_x = x/Math.cos(y0); 

      double foundLatitude = new_x + x0; 
      double foundLongitude = y + y0; 
      LatLng randomLatLng = new LatLng(foundLatitude, foundLongitude); 

      Location loc = new Location(""); 
      loc.setLatitude(randomLatLng.latitude); 
      loc.setLongitude(randomLatLng.longitude); 

//dont know what to return 
     return ; 
    } 

    public final void addMarker(GoogleMap mMap) { 
//dont know how to get working the getRandomLocation()) 
     mMap.addMarker(new MarkerOptions() 
       // .position(new LatLng(48.349723, 18.052405)) 
       .position(getRandomLocation()) 
       .title("krokodíl") 
       .icon(BitmapDescriptorFactory.fromBitmap(resizeMapIcons("krokodil", 100, 100)))); 

     mMap.addMarker(new MarkerOptions() 
       // .position(new LatLng(48.310025, 18.038878)) 
       .position(getRandomLocation()) 
       .icon(BitmapDescriptorFactory.fromBitmap(resizeMapIcons("fretka", 100, 100))) 
       .title("fretk")); 
     mMap.addMarker (new MarkerOptions() 
       .title("hroch") 
       .icon(BitmapDescriptorFactory.fromBitmap(resizeMapIcons("hroch", 100, 100))) 
       // .title() 
       //.snippet(mObj.getNumber(Configs.MONSTERS_MONSTER_POINTS) + " points") 
       // .position(new LatLng(48.318569, 18.055767))); 
       .position(getRandomLocation())); 
    } 

回答

1

根据您的代码的逻辑,您需要返回randomLatLng

更改行:

public Location getRandomLocation() { 

到:

public LatLng getRandomLocation() { 

,使return语句是这样的:

return randomLatLng; 

MarkerOptions.position()需要LatLng类型的对象的方法,即是你的IDE显示你的错误。

+0

是的,我明白,但在创建markeroptions我不知道要在.possition(getRandomLocation)中添加什么; –

+0

.position(getRandomLocation())是正确的。 –

+0

谢谢这个问题是不是在返回statemant或markeroptions我得到不好的声明methodit必须是LatLng –