我在Android设备上使用谷歌地图,我从标记获取信息,我能够获得标记标题和制作者片段,但无法获取电话号码信息,请提前帮助我,谢谢。
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();
}
}
答案 0 :(得分:1)
您应该split
Snippet
Marker
个文字并提取所有详细信息。
见下文
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();
}