【问题标题】:Display map on basis of previous activity根据以前的活动显示地图
【发布时间】:2013-07-04 14:34:32
【问题描述】:

如何通过单击先前活动的按钮来显示特定城市的地图..例如:在活动一中,我在编辑文本中键入纽约并单击按钮,它打开包含地图的活动二并指向纽约市。 ..我也想用不同的标记来展示纽约的旅游景点..有人可以给我指路吗..提前谢谢

我已经完成了显示附近位置的地图,尽管它没有更新位置.. :( 任何帮助都将不胜感激

【问题讨论】:

    标签: android google-maps google-places-api


    【解决方案1】:

    最简单的方法是将城市名称传递给地图活动。

    在活动一中执行以下操作:

    Intent i = new Intent(getApplicationContext(), MapActivity.class);
    i.putExtra("location","New York");
    startActivity(i);
    

    要检索值,请在 Map 活动中执行以下操作:

    String location = getIntent().getExtras().getString("location");
    

    【讨论】:

    • 我知道如何在意图中传递数据,但是你将如何使用字符串位置来显示纽约的地图?这就是我想知道在意图中传递位置后会发生什么......
    【解决方案2】:

    你在一个问题中问了很多问题。

    要通过地址查找城市并在地图上显示它,您需要创建一个Geocoder,它将给出城市坐标,您可以查看本教程:

    http://wptrafficanalyzer.in/blog/android-geocoding-showing-user-input-location-on-google-map-android-api-v2/

    这是一个 AsyncTask 代码 sn-p,用于访问 Geocoder 服务:

    // An AsyncTask class for accessing the GeoCoding Web Service
    private class GeocoderTask extends AsyncTask<String, Void, List<Address>>{
    
        @Override
        protected List<Address> doInBackground(String... locationName) {
            // Creating an instance of Geocoder class
            Geocoder geocoder = new Geocoder(getBaseContext());
            List<Address> addresses = null;
    
            try {
                // Getting a maximum of 3 Address that matches the input text
                addresses = geocoder.getFromLocationName(locationName[0], 3);
            } catch (IOException e) {
                e.printStackTrace();
            }
            return addresses;
        }
    
        @Override
        protected void onPostExecute(List<Address> addresses) {
    
            if(addresses==null || addresses.size()==0){
                Toast.makeText(getBaseContext(), "No Location found", Toast.LENGTH_SHORT).show();
            }
    
            // Clears all the existing markers on the map
            googleMap.clear();
    
            // Adding Markers on Google Map for each matching address
            for(int i=0;i<addresses.size();i++){
    
                Address address = (Address) addresses.get(i);
    
                // Creating an instance of GeoPoint, to display in Google Map
                latLng = new LatLng(address.getLatitude(), address.getLongitude());
    
                String addressText = String.format("%s, %s",
                address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : "",
                address.getCountryName());
    
                markerOptions = new MarkerOptions();
                markerOptions.position(latLng);
                markerOptions.title(addressText);
    
                googleMap.addMarker(markerOptions);
    
                // Locate the first location
                if(i==0)
                    googleMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
            }
        }
    }
    

    现在对于旅游景点,您将不得不查询某种服务,该服务将为您提供此类信息,并对其进行解析并将其显示在地图上。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-07
      • 2018-01-03
      • 2016-04-10
      • 2012-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多