首页 > 解决方案 > 通过接收纬度和经度在地图上添加标记另一个活动

问题描述

我在主要活动中有一个片段,其中包含地图。默认情况下,我在印度博帕尔市添加了一个标记。我有一个导航抽屉,其中有一个选项设置位置。现在,当我输入城市名称时,我希望我的标记移动到该位置。我为此使用了 Intent,但这没有用。我的片段代码是

public class MyFragment extends Fragment implements OnMapReadyCallback {
GoogleMap mgoogleMap;
MapView mapView;
View view;
Double Lat,Longg;
int c = 0;
public MyFragment(){


}
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    }
@Override
public View onCreateView(LayoutInflater inflater , ViewGroup container, Bundle savedInstanceState){
  /*  Lat = Double.parseDouble(getArguments().getString("Lattitude"));
    Longg = Double.parseDouble(getArguments().getString("Longitude"));
    if(Lat!=null)
        c++;*/
    view = inflater.inflate(R.layout.content_main,container , false);
    return view;

}

@Override
public void onViewCreated(View view,Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    mapView = view.findViewById(R.id.map);
    if(mapView!=null)
    mapView.onCreate(null);
    mapView.onResume();
    mapView.getMapAsync(this);
}

@Override
public void onMapReady(GoogleMap googleMap) {
    MapsInitializer.initialize(getContext());
    mgoogleMap = googleMap;
    googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    if (c == 0) {
        googleMap.addMarker(new MarkerOptions().position(new LatLng(23.2599, 77.4126)).title("BHOPAL"));
        CameraPosition bhopal = CameraPosition.builder().target(new LatLng(23.2599, 77.4126)).zoom(16).bearing(0).build();
        googleMap.moveCamera(CameraUpdateFactory.newCameraPosition(bhopal));
    } else {
        googleMap.addMarker(new MarkerOptions().position(new LatLng(Lat, Longg)));
        CameraPosition city = CameraPosition.builder().target(new LatLng(Lat, Longg)).zoom(16).bearing(0).build();
        googleMap.moveCamera(CameraUpdateFactory.newCameraPosition(city));
    }
}

}

我的位置活动是:

public class LocationActivtiy extends AppCompatActivity {
private EditText place;
private EditText lat,longi;
private double latt,longg;
private Button done;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_location_activtiy);
    place = findViewById(R.id.loc);
    lat = findViewById(R.id.lat);
    longi = findViewById(R.id.longi);
    done = findViewById(R.id.done);
    List<Address>addresses = null;
    if(Geocoder.isPresent()){
        try {
            if(place.getText()!=null) {
                String location = place.getText().toString();
                Geocoder gc = new Geocoder(this);
                addresses = gc.getFromLocationName(location, 1);
                Address addr = addresses.get(0);
                latt = addr.getLatitude();
                longg = addr.getLongitude();
            }else if(lat.getText()!=null){
                latt = Double.parseDouble(lat.getText().toString());
                longg = Double.parseDouble(longi.getText().toString());
            }
            else{
                Toast.makeText(this,"Enter any one 
value",Toast.LENGTH_LONG).show();
            }
                }
     catch (IOException e) {
         Toast.makeText(this,"Services Not 
available",Toast.LENGTH_LONG).show();
        }
    }
    done.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new 
Intent(LocationActivtiy.this,MyFragment.class);
            Bundle bundle = new Bundle();
            bundle.putDouble("Lattitude",latt);
            bundle.putDouble("Longitude",longg);
            MyFragment myFragment = new MyFragment();
            myFragment.setArguments(bundle);
            startActivity(intent);
        }
    });
}
}

我有 2 个选项,我可以通过城市名称或纬度或经度移动到该位置。但这些来自不同的活动。此外,如果我的应用程序第一次打开默认标记应设置在博帕尔位置。

标签: androidgoogle-mapsfragment

解决方案


推荐阅读