Geocoder 真的可以与 google map api v2 一起使用吗?
Does Geocoder actually work with google map api v2?
我正在尝试将 Geocoder 与我的 google 地图一起使用。我可以从位置名称中获取经纬度。我的问题是我无法使用纬度和经度添加任何标记或将相机移动到该位置。每次都显示空指针异常。但我得到的纬度和经度作为文本视图。我已经使用了所有权限,实际上 Geocoder 工作正常但不适用于 google 地图。
Geocoder 无法添加任何标记或移动到 google 地图上的那个地方吗?
我正在使用 fragment class 因为导航抽屉,它是否会造成这个问题?
如果有人有任何答案,那将很有帮助。
如果有人问,我可以 post 我的工作。
谢谢
这是我的片段class
public class GMapFragment extends Fragment implements OnMapReadyCallback{
private GoogleMap googleMap;
MapFragment fragment;
EditText location_tf;
Button button;
List<Address> addressList;
Geocoder geocoder;
double lat;
double lng;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_gmap, container, false);
location_tf = (EditText)rootView.findViewById (R.id.TFaddress);
button = (Button)rootView.findViewById(R.id.Bsearch);
button.setOnClickListener(buttonOnClickListener);
initilizeMap();
return rootView;}
OnClickListener buttonOnClickListener =
new OnClickListener(){
public void onClick(View v)
{
String location = location_tf.getText().toString();
Geocoder geocoder = new Geocoder(getActivity(),Locale.ENGLISH);
List<Address> addressList = null;
try {
addressList = geocoder.getFromLocationName(location, 1);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Address address = addressList.get(0);
String locality= address.getLocality();
Toast.makeText(getActivity(), locality, Toast.LENGTH_SHORT).show();
lat = address.getLatitude();
lng = address.getLongitude();
LatLng ll = new LatLng(lat, lng);
googleMap.addMarker(new MarkerOptions() //line 89
.position((ll)));
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(
(ll), 15));
}
}
;
private void initilizeMap()
{
MapFragment fragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
if (fragment != null){
fragment.getMapAsync(this);
}
}
@Override
public void onMapReady(final GoogleMap googleMap) {
googleMap.setMyLocationEnabled(true);
googleMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
}
@Override
public void onPause() {
final FragmentManager fragManager = this.getFragmentManager();
final Fragment fragment = fragManager.findFragmentById(R.id.map);
if(fragment!=null){
fragManager.beginTransaction().remove( fragment).commit();
super.onPause();
}
}
@Override
public void onDestroy() {
super.onDestroy();
final FragmentManager fragManager = this.getFragmentManager();
final Fragment fragment = fragManager.findFragmentById(R.id.map);
if(fragment!=null){
fragManager.beginTransaction().remove(fragment).commit();
}
}
}
这是我的LogCat
05-23 20:05:36.133: E/AndroidRuntime(10092): FATAL EXCEPTION: main
05-23 20:05:36.133: E/AndroidRuntime(10092): Process: com.cubecode.mapdhaka, PID: 10092
05-23 20:05:36.133: E/AndroidRuntime(10092): java.lang.NullPointerException
05-23 20:05:36.133: E/AndroidRuntime(10092): at com.cubecode.mapdhaka.GMapFragment.onClick(GMapFragment.java:89)
05-23 20:05:36.133: E/AndroidRuntime(10092): at android.view.View.performClick(View.java:4472)
05-23 20:05:36.133: E/AndroidRuntime(10092): at android.view.View$PerformClick.run(View.java:18779)
05-23 20:05:36.133: E/AndroidRuntime(10092): at android.os.Handler.handleCallback(Handler.java:808)
05-23 20:05:36.133: E/AndroidRuntime(10092): at android.os.Handler.dispatchMessage(Handler.java:103)
05-23 20:05:36.133: E/AndroidRuntime(10092): at android.os.Looper.loop(Looper.java:193)
05-23 20:05:36.133: E/AndroidRuntime(10092): at android.app.ActivityThread.main(ActivityThread.java:5292)
05-23 20:05:36.133: E/AndroidRuntime(10092): at java.lang.reflect.Method.invokeNative(Native Method)
05-23 20:05:36.133: E/AndroidRuntime(10092): at java.lang.reflect.Method.invoke(Method.java:515)
05-23 20:05:36.133: E/AndroidRuntime(10092): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
05-23 20:05:36.133: E/AndroidRuntime(10092): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
05-23 20:05:36.133: E/AndroidRuntime(10092): at dalvik.system.NativeStart.main(Native Method)
googleMap 变量为空,因为您从未分配该变量。
添加onMapReady:
this.googleMap = googleMap;
我正在尝试将 Geocoder 与我的 google 地图一起使用。我可以从位置名称中获取经纬度。我的问题是我无法使用纬度和经度添加任何标记或将相机移动到该位置。每次都显示空指针异常。但我得到的纬度和经度作为文本视图。我已经使用了所有权限,实际上 Geocoder 工作正常但不适用于 google 地图。 Geocoder 无法添加任何标记或移动到 google 地图上的那个地方吗?
我正在使用 fragment class 因为导航抽屉,它是否会造成这个问题?
如果有人有任何答案,那将很有帮助。 如果有人问,我可以 post 我的工作。 谢谢
这是我的片段class
public class GMapFragment extends Fragment implements OnMapReadyCallback{
private GoogleMap googleMap;
MapFragment fragment;
EditText location_tf;
Button button;
List<Address> addressList;
Geocoder geocoder;
double lat;
double lng;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_gmap, container, false);
location_tf = (EditText)rootView.findViewById (R.id.TFaddress);
button = (Button)rootView.findViewById(R.id.Bsearch);
button.setOnClickListener(buttonOnClickListener);
initilizeMap();
return rootView;}
OnClickListener buttonOnClickListener =
new OnClickListener(){
public void onClick(View v)
{
String location = location_tf.getText().toString();
Geocoder geocoder = new Geocoder(getActivity(),Locale.ENGLISH);
List<Address> addressList = null;
try {
addressList = geocoder.getFromLocationName(location, 1);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Address address = addressList.get(0);
String locality= address.getLocality();
Toast.makeText(getActivity(), locality, Toast.LENGTH_SHORT).show();
lat = address.getLatitude();
lng = address.getLongitude();
LatLng ll = new LatLng(lat, lng);
googleMap.addMarker(new MarkerOptions() //line 89
.position((ll)));
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(
(ll), 15));
}
}
;
private void initilizeMap()
{
MapFragment fragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
if (fragment != null){
fragment.getMapAsync(this);
}
}
@Override
public void onMapReady(final GoogleMap googleMap) {
googleMap.setMyLocationEnabled(true);
googleMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
}
@Override
public void onPause() {
final FragmentManager fragManager = this.getFragmentManager();
final Fragment fragment = fragManager.findFragmentById(R.id.map);
if(fragment!=null){
fragManager.beginTransaction().remove( fragment).commit();
super.onPause();
}
}
@Override
public void onDestroy() {
super.onDestroy();
final FragmentManager fragManager = this.getFragmentManager();
final Fragment fragment = fragManager.findFragmentById(R.id.map);
if(fragment!=null){
fragManager.beginTransaction().remove(fragment).commit();
}
}
}
这是我的LogCat
05-23 20:05:36.133: E/AndroidRuntime(10092): FATAL EXCEPTION: main
05-23 20:05:36.133: E/AndroidRuntime(10092): Process: com.cubecode.mapdhaka, PID: 10092
05-23 20:05:36.133: E/AndroidRuntime(10092): java.lang.NullPointerException
05-23 20:05:36.133: E/AndroidRuntime(10092): at com.cubecode.mapdhaka.GMapFragment.onClick(GMapFragment.java:89)
05-23 20:05:36.133: E/AndroidRuntime(10092): at android.view.View.performClick(View.java:4472)
05-23 20:05:36.133: E/AndroidRuntime(10092): at android.view.View$PerformClick.run(View.java:18779)
05-23 20:05:36.133: E/AndroidRuntime(10092): at android.os.Handler.handleCallback(Handler.java:808)
05-23 20:05:36.133: E/AndroidRuntime(10092): at android.os.Handler.dispatchMessage(Handler.java:103)
05-23 20:05:36.133: E/AndroidRuntime(10092): at android.os.Looper.loop(Looper.java:193)
05-23 20:05:36.133: E/AndroidRuntime(10092): at android.app.ActivityThread.main(ActivityThread.java:5292)
05-23 20:05:36.133: E/AndroidRuntime(10092): at java.lang.reflect.Method.invokeNative(Native Method)
05-23 20:05:36.133: E/AndroidRuntime(10092): at java.lang.reflect.Method.invoke(Method.java:515)
05-23 20:05:36.133: E/AndroidRuntime(10092): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
05-23 20:05:36.133: E/AndroidRuntime(10092): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
05-23 20:05:36.133: E/AndroidRuntime(10092): at dalvik.system.NativeStart.main(Native Method)
googleMap 变量为空,因为您从未分配该变量。
添加onMapReady:
this.googleMap = googleMap;