我想在我的應用程序內部實現谷歌地圖,並試圖將字符串轉換爲lat和long,例如「USA New York - > 2345.423423」。我在互聯網上搜索過,我發現了很多例子來將lat和long轉換爲String,但我不明白爲什麼,因爲我甚至不知道我居住的地址的經緯度。 ..我發現了一些東西,它可以工作,但它轉換成地址列表。如果可能的話,我希望更容易些。謝謝你,有一個愉快的一天將字符串轉換爲經度和緯度
package com.currencymeeting.activities;
import java.io.IOException;
import java.util.List;
import android.location.Address;
import android.location.Geocoder;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.Toast;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class GoogleMapsActivity extends FragmentActivity{
GoogleMap googleMap;
MarkerOptions markerOptions;
LatLng latLng;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_google_maps);
SupportMapFragment supportMapFragment = (SupportMapFragment)
getSupportFragmentManager().findFragmentById(R.id.googleMap);
googleMap = supportMapFragment.getMap();
new GeocoderTask().execute("USA New York");
}
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], 1);
} 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.newLatLngZoom(latLng,16));
}
}
}
}
所以基本上,您希望我們爲您自己的目的編輯其他人的代碼? – HavelTheGreat 2015-02-06 19:48:06
正如@Elizion暗示的那樣,這不應該做太多的修改來做你想做的事情,看起來你沒有真正嘗試過在教程之外進行復制。 – zgc7009 2015-02-06 19:50:35
哦,我的天..我修改了它..我想從一個字符串中創建一個地址,而不是從列表<> ...如果我刪除這些註釋,您感覺會更好......? – Mike 2015-02-06 19:56:56