【问题标题】:Sending search parameter from activity to google maps api (Android studio)将搜索参数从活动发送到谷歌地图 api(Android 工作室)
【发布时间】:2015-08-19 13:11:21
【问题描述】:

首先,我只想说我不擅长解释(也不擅长编程),所以请耐心等待。

布局:

我有两个活动。 第一个是复选框和按钮所在的主要活动。 第二个活动是 google maps api。

进展:

当我点击按钮时,我进入了谷歌地图活动(使用意图)并有一个固定的位置。

问题:

如果在按下按钮时选中复选框,我想向谷歌地图发送位置搜索(例如:如果复选框显示保龄球,则搜索将是保龄球),但我没有不知道怎么写,也不知道在哪里写这段代码。

感谢任何链接/提示/答案:)

MainActivity

private static Button button_Ok;
private static CheckBox bowling;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    onClickButtonListener();
}

public void onClickButtonListener() {
    button_Ok = (Button)findViewById(R.id.button);
    bowling = (CheckBox)findViewById(R.id.checkBox);
    button_Ok.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent("com.google.android.gms.maps.SupportMapFragment");
            startActivity(intent);
        }
    });
}

地图活动

private void setUpMap() {
    mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
    mMap.setMyLocationEnabled(true);

    // Get LocationManager object from System Service LOCATION_SERVICE
    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    // Create a criteria object to retrieve provider
    Criteria criteria = new Criteria();

    // Get the name of the best provider
    String provider = locationManager.getBestProvider(criteria, true);

    // Get Current Location
    Location myLocation = locationManager.getLastKnownLocation(provider);

    // set map type
    mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);

    // Get latitude of the current location
    double latitude = *fixed*; //myLocation.getLatitude();

    // Get longitude of the current location
    double longitude = *fixed*; //myLocation.getLongitude();

    // Create a LatLng object for the current location
    LatLng latLng = new LatLng(latitude, longitude);

    // Show the current location in Google Map
    mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));

    // Zoom in the Google Map
    mMap.animateCamera(CameraUpdateFactory.zoomTo(14));
    mMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).title("You are here!").snippet("Consider yourself located"));

    LatLng myCoordinates = new LatLng(latitude, longitude);
    CameraUpdate yourLocation = CameraUpdateFactory.newLatLngZoom(myCoordinates, 12);
    mMap.animateCamera(yourLocation);

    CameraPosition cameraPosition = new CameraPosition.Builder()
            .target(myCoordinates)      // Sets the center of the map to LatLng (refer to previous snippet)
            .zoom(13)                   // Sets the zoom
            .bearing(90)                // Sets the orientation of the camera to east
            .tilt(0)                   // Sets the tilt of the camera to 30 degrees
            .build();                   // Creates a CameraPosition from the builder
    mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

    // ... get a map.
    Circle circle = mMap.addCircle(new CircleOptions()
            .center(new LatLng(*fixed*, *fixed*))
            .radius(100)
            .strokeColor(Color.RED)
            .fillColor(Color.TRANSPARENT));


    mMap.getUiSettings().setZoomControlsEnabled(true);
}

}

我设置了固定的 LatLng,因为获取位置功能在模拟器上似乎不起作用。

【问题讨论】:

  • 请发布您的代码。
  • 你的问题太笼统了。这是解决您问题的教程:wptrafficanalyzer.in/blog/…
  • @RajanBhavsar 对不起,它说要尽量保持一般性:) 感谢教程,我会读一读:) 但是我不希望用户编写搜索,关键是让他们选择其中一个复选框:) 再次抱歉,感谢您的帮助,我会记住的!

标签: java android google-maps android-studio


【解决方案1】:

我不确定您将使用的代码,因为有十几种不同的方法可以做到这一点,但我可以建议我将如何实现此功能。

我会有一个活动MapActivity 和两个片段。一个片段包含复选框和搜索按钮 (buttonFragment),第二个片段将显示您的 Google 地图 (mapFragment)。这样做,您只需初始化和设置一次 GoogleMaps,并且可以从 buttonFragment 访问 API 的功能。

按下按钮后,您将使用 GoogleMaps 获取您的位置,使用 Google Places API 您可以返回附近地点的列表,然后您可以使用接口回调将此信息传递给您的活动,然后会启动 mapFragment并传递从 buttonFragment 收到的信息。

谷歌地图 API: https://developers.google.com/maps/

Google 地方信息 API: https://developers.google.com/places/

【讨论】:

  • 由于我是这个场景的新手,我真的不知道该怎么做 :) 我将研究解决它的碎片方式,因为它似乎走上了正确的道路: ) 感谢您的帮助!
猜你喜欢
  • 2020-03-14
  • 2016-12-23
  • 1970-01-01
  • 2021-06-27
  • 2012-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多