Google API 返回服务器位置,而不是用户位置

Google API returning the server location, not user location

你好,虽然我是通过这段代码获取用户位置,但我实际上是在获取服务器的位置,知道如何更改它以便获取用户位置吗?

    public void geolocate() {
            try {

            GeolocationPayloadBuilder payloadBuilder = new GeolocationPayload.GeolocationPayloadBuilder();
            GeolocationPayload payload = payloadBuilder.createGeolocationPayload();
            //GeoApiContext context = new GeoApiContext.Builder().apiKey("my api key").build();
            // I guess the payload needs to be build in a different way but no clue how it should be :/
            GeolocationApiRequest req = (GeolocationApiRequest) GeolocationApi.geolocate(context, payload);

            GeolocationResult res = req.await();
            String location = res.location.toString();
            String[] latLngArray = location.split(",");
            com.google.maps.model.LatLng latLng = new com.google.maps.model.LatLng(Double.parseDouble(latLngArray[0]),
                    Double.parseDouble(latLngArray[1]));
            GeocodingApiRequest geoReq = GeocodingApi.reverseGeocode(context, latLng);
            GeocodingResult[] geoRes = geoReq.await();
            // Setting the user location for view
            System.out.println(geoRes[0].formattedAddress);
            origen.setValue(geoRes[0].formattedAddress);

        } catch (Exception e) {
            System.out.println("Exception in NetClientGet:- " + e);
        }
        }

这是我从中获取对象的依赖项:

<dependency>
            <groupId>com.google.maps</groupId>
            <artifactId>google-maps-services</artifactId>
            <version>0.9.3</version>
</dependency>

希望有人能帮我解决这个问题,在此先感谢

编辑:

我一直在搜索并找到如何在 https://developers.google.com/maps/documentation/geolocation/intro#cell_tower_object

的帮助下构建有效载荷

但我有几个问题,即如何让我的用户 mac 地址创建 wifiAccessPoint,以及我在哪里可以找到我所在城市(哥伦比亚卡利)的手机信号塔的信息?只是更新将继续搜索任何帮助表示赞赏..

@POST
@Path("/geolocate")
public String geolocate() {
    try {
        CellTower newCellTower = new CellTower.CellTowerBuilder().CellId(42).LocationAreaCode(415)
                .MobileCountryCode(310).MobileNetworkCode(410).Age(0).createCellTower();
        WifiAccessPoint newWifiAccessPoint = new WifiAccessPoint.WifiAccessPointBuilder()
                .MacAddress("00:25:9c:cf:1c:ac").createWifiAccessPoint();
        WifiAccessPoint newWifiAccessPoint2 = new WifiAccessPoint.WifiAccessPointBuilder()
                .MacAddress("00:25:9c:cf:1c:ad").createWifiAccessPoint();

        GeolocationPayloadBuilder payloadBuilder = new GeolocationPayload.GeolocationPayloadBuilder()
                .HomeMobileCountryCode(310).HomeMobileNetworkCode(410).RadioType("gsm").Carrier("Vodafone")
                .ConsiderIp(false).AddCellTower(newCellTower).AddWifiAccessPoint(newWifiAccessPoint)
                .AddWifiAccessPoint(newWifiAccessPoint2);
        GeolocationPayload payload = payloadBuilder.createGeolocationPayload();
        GeoApiContext context = new GeoApiContext.Builder().apiKey("my api key")
                .build();

        GeolocationApiRequest req = (GeolocationApiRequest) GeolocationApi.geolocate(context, payload);

        GeolocationResult res = req.await();
        String location = res.location.toString();
        String[] latLngArray = location.split(",");
        com.google.maps.model.LatLng latLng = new com.google.maps.model.LatLng(Double.parseDouble(latLngArray[0]),
                Double.parseDouble(latLngArray[1]));
        GeocodingApiRequest geoReq = GeocodingApi.reverseGeocode(context, latLng);
        GeocodingResult[] geoRes = geoReq.await();
        // Setting the user location for view
        return geoRes[0].formattedAddress;

    } catch (Exception e) {
        System.out.println("Exception in NetClientGet:- " + e);
    }
    return "XD";
}

Geolocation API of Google Maps Platform is not intended for getting user location, what I can suggest is that you use the HTML5 Geolocation instead, there's also a sample of that in the Google Maps Platform documentation. But please note that this is not supported by Google as this is using HTML5 Geolocation and not Google APIs, if you wish to get the address of the user location as well, you may Geocode the coordinates that will be returned by the HTML5 Geolocation. You may see the sample below (without the Geocoding function). Here's a working sample - https://jsfiddle.net/w2sad5pn/

if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
      var pos = {
        lat: position.coords.latitude,
        lng: position.coords.longitude
      };

      infoWindow.setPosition(pos);
      infoWindow.setContent('Location found.');
      infoWindow.open(map);
      map.setCenter(pos);
    }, function() {
      handleLocationError(true, infoWindow, map.getCenter());
    });
  } else {
    // Browser doesn't support Geolocation
    handleLocationError(false, infoWindow, map.getCenter());
  }
}