【问题标题】:Get phone number from google map marker从谷歌地图标记获取电话号码
【发布时间】:2014-07-08 07:06:43
【问题描述】:

我在 android 设备上使用谷歌地图,我从 markes 获取信息,我能够获取标记标题和制造商 sn-p,但无法获取电话号码信息,请帮助我,提前谢谢。

package com.avion.mapdemo;

公共类 MainActivity 扩展 Activity 实现 OnInfoWindowClickListener {

// Google Map
private GoogleMap googleMap;
MarkerOptions markerOptions;
LatLng latLng;
GPSTracker gps;
Address adrs;

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

    try {
        // Loading map

        initilizeMap();

        // my location...
        googleMap.setMyLocationEnabled(true);

        // room setting from 2(min) to 21 (max)..
        googleMap.animateCamera(CameraUpdateFactory.zoomTo(10.0f));

        // get my location address......

        myLocation();

    } catch (Exception e) {
        e.printStackTrace();
    }

    // marker information tab clicked...........

    googleMap.setOnInfoWindowClickListener(this);

}

// method my location
private void myLocation() {

    // TODO Auto-generated method stub

    // for logitude and latitude..........

    gps = new GPSTracker(MainActivity.this);

    // check if GPS enabled
    if (gps.canGetLocation()) {

        double latitude = gps.getLatitude();
        double longitude = gps.getLongitude();

        //double latitude = 40.71958;
        // double longitude = -74.09595;


        // Toast.makeText(getApplicationContext(),latitude
        // +"--"+longitude,Toast.LENGTH_SHORT).show();

        // for address..........
        Geocoder geocoder;
        List<Address> addresses = null;
        geocoder = new Geocoder(this, Locale.getDefault());
        try {
            addresses = geocoder.getFromLocation(latitude, longitude, 1);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        String address = addresses.get(0).getAddressLine(0);
        String city = addresses.get(0).getAddressLine(1);
        String country = addresses.get(0).getAddressLine(2);

        Toast.makeText(getApplicationContext(),
                address + "" + city + "" + country, Toast.LENGTH_LONG)
                .show();

        Log.e("strite", address);
        Log.e("city", city);
        Log.e("country", country);
        // search string.....
        // String addr="Hotel "+address
        // +" "+city+" "+country;

        String addr = "Bar" + " " + city + " " + country;

        // call async task for load bars.....

        new GeocoderTask().execute(addr);

    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

/**
 * function to load map. If map is not created it will create it for you
 * */
private void initilizeMap() {
    if (googleMap == null) {
        googleMap = ((MapFragment) getFragmentManager().findFragmentById(
                R.id.map)).getMap();

        // check if map is created successfully or not
        if (googleMap == null) {
            Toast.makeText(getApplicationContext(),
                    "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                    .show();
        }
    }
}

@Override
protected void onResume() {
    super.onResume();
    initilizeMap();
}

// 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 transforming a street address or other description of a
        // location
        // into a (latitude, longitude) coordinate.
        Geocoder geocoder = new Geocoder(getBaseContext());
        List<Address> addresses = null;

        try {
            // Getting a maximum of 10 Address that matches the input text
            addresses = geocoder.getFromLocationName(locationName[0], 10);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return addresses;
    }

    @Override
    protected void onPostExecute(List<Address> addresses) {

        if (addresses == null || addresses.size() == 0) {
            Toast.makeText(getBaseContext(), "No Bar 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 Strings describing a location
            adrs = (Address) addresses.get(i);

            // Creating an instance of GeoPoint, to display in Google Map
            // latLng class representing a pair of latitude and longitude
            // coordinates,

            latLng = new LatLng(adrs.getLatitude(), adrs.getLongitude());

            // latLng = new LatLng(40.71958,-74.09595);

            String addressText = String.format("%s, %s", adrs
                    .getMaxAddressLineIndex() > 0 ? adrs.getAddressLine(0)
                    : "", adrs.getCountryName());

            // markerOptions to add property to marker

            markerOptions = new MarkerOptions();
            markerOptions.position(latLng);
            markerOptions.title(adrs.getAddressLine(0));
            markerOptions.snippet(adrs.getAddressLine(1) + ", "
                    + adrs.getAddressLine(2) + ", "
                    + adrs.getPhone());

            /*
             * getAddressLine(0) location name .
             * getAddressLine(1) local address .
             * getAddressLine(2) city,state .
             * getAddressLine(3) country .
             */
            googleMap.addMarker(markerOptions);

            // Locate the first location
            if (i == 0)
                googleMap.animateCamera(CameraUpdateFactory
                        .newLatLng(latLng));
        }
        // end of loop.....
    }
}

// on marker info tab click..
@Override
public void onInfoWindowClick(Marker marker) {

    Toast.makeText(this, marker.getTitle() + "--" + marker.getSnippet(),
            Toast.LENGTH_LONG).show();

}

}

【问题讨论】:

  • 告诉我你的代码如何获取信息?
  • 最后我必须添加电话号码
  • 任何帮助完整代码或网站链接???
  • 请将您的代码发布在您获得此信息的位置?我的意思是任何Custom WindowMarker Click
  • 请检查代码...

标签: android google-maps map infowindow marker


【解决方案1】:

你应该split你的SnippetMarker文本并提取所有细节。

见下文

public void onInfoWindowClick(Marker marker) {

String[] str2 = marker.getSnippet().split(",");
String Addressline1=str2[0]; //Addressline 1
String Addressline2=str2[1]; //Addressline 2
String phone=str2[2]; //Phone

Toast.makeText(this, marker.getTitle() + "--" + marker.getSnippet()+"-- "+phone,
        Toast.LENGTH_LONG).show();
}

【讨论】:

  • 没有朋友,它只是显示地址的一部分,他们有什么内置功能吗?
  • @user3395433 这是因为你必须使用, with one space concat 你的字符串。明白我在说什么吗?在您的代码中仔细检查它。您必须使用single ,(comma) 无空格/ 联系
  • no frd,我也试过了,问题是我的连接字符串也没有手机号码,所以它们与连接无关
猜你喜欢
  • 1970-01-01
  • 2012-06-06
  • 2020-04-28
  • 1970-01-01
  • 2021-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-23
相关资源
最近更新 更多