我尝试在我的谷歌地图上使用Geocoder。我可以从位置名称获取纬度和经度。我的问题是我不能使用纬度和经度来添加任何标记或将相机移动到该位置。每次显示空指针异常。但是我将纬度和经度视为文本视图。我已经使用了所有权限,实际上Geocoder工作正常,但没有谷歌地图。 没有地理编码器可以在Google地图上添加任何标记或移动到该位置吗?
由于导航抽屉我正在使用片段类,它有什么可以创建这个问题吗?
如果有任何机构有任何答案,那将会有所帮助。 如果有人问我,我可以发布我的作品。 谢谢
这是我的片段类
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$1.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)
答案 0 :(得分:0)
googleMap变量为null,因为您从不分配变量。
添加onMapReady:
HttpWebRequest