[英]Set marker with click on map OpenStreetMap In Android
如何在Android或Xamarin.Android中的開放街道地圖上單擊地圖並添加書簽
地圖活動的XML布局還具有搜索編輯文本,以使用Google地圖搜索地點。 地圖活動的XML布局應包含
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent">
<TextView android:id="@+id/addressTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".AppComponents.Activities.LocationPickerActivity" />
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="4dp"
android:paddingTop="4dp"
card_view:cardElevation="4dp"
card_view:cardMaxElevation="4dp"
card_view:cardUseCompatPadding="true"
card_view:cardCornerRadius="4dp">
<fragment
android:id="@+id/place_autocomplete_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
/>
</android.support.v7.widget.CardView>
<Button
android:id="@+id/locationSetButton"
android:layout_width="wrap_content"
android:elevation="8dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="17dp"
android:layout_marginStart="27dp"
android:text="Set Location" />
</RelativeLayout>
在LocationPicker.java類中,您必須聲明Marker&GoogleMap類型的變量。
package com.app1.myapplication.AppComponents.Activities;
import android.app.Activity;
import android.content.Intent;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
import com.google.android.gms.common.GooglePlayServicesRepairableException;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.Status;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationCallback;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationResult;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.location.places.Place;
import com.google.android.gms.location.places.Places;
import com.google.android.gms.location.places.ui.PlaceAutocomplete;
import com.google.android.gms.location.places.ui.PlaceAutocompleteFragment;
import com.google.android.gms.location.places.ui.PlacePicker;
import com.google.android.gms.location.places.ui.PlaceSelectionListener;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.app1.myapplication.R;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
public class LocationPicker extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
Marker markerLocation;
private GoogleApiClient mGoogleApiClient;
private FusedLocationProviderClient mFusedLocationClient;
private LocationRequest mLocationRequest;
private LocationCallback mLocationCallback;
double a,b;
Place place;
LatLng finalLocation;
private LatLngBounds.Builder mBounds = new LatLngBounds.Builder();
private void addPointToViewPort(LatLng newPoint) {
mBounds.include(newPoint);
mMap.animateCamera(CameraUpdateFactory.newLatLngBounds(mBounds.build(),
findViewById(R.id.addressTextView).getHeight()));
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if (resultCode == Activity.RESULT_OK) {
place = PlacePicker.getPlace(this, data);
PlaceAutocomplete.getPlace(this,data);
} else if (resultCode == PlacePicker.RESULT_ERROR) {
Toast.makeText(this, "Places API failure! Check that the API is enabled for your key",
Toast.LENGTH_LONG).show();
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
try {
Intent intent = new PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_OVERLAY).build(this);
startActivityForResult(intent,1);
}
catch (GooglePlayServicesRepairableException e){
}
catch (GooglePlayServicesNotAvailableException e){
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_location_picker);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Places.GEO_DATA_API)
.build();
mGoogleApiClient.connect();
View locationButton = ((View) findViewById(Integer.parseInt("1")).getParent()).findViewById(Integer.parseInt("2"));
RelativeLayout.LayoutParams rlp = (RelativeLayout.LayoutParams) locationButton.getLayoutParams();
// position on right bottom
rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0);
rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
rlp.setMargins(0, 1500, 30, 20);
PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment)
getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);
//Task<AutocompletePredictionBufferResponse> results=Places.GeoDataApi.getAutocompletePredictions()
autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
@Override
public void onPlaceSelected(Place place2) {
place=place2;
finalLocation=place2.getLatLng();
mMap.moveCamera(CameraUpdateFactory.newLatLng(place2.getLatLng()));
mMap.animateCamera(CameraUpdateFactory.zoomTo(17));
try{
mMap.setMyLocationEnabled(true);
}
catch(SecurityException e){
}
// TODO: Get info about the selected place.
//Log.i(TAG, "Place: " + place.getName());
}
@Override
public void onError(Status status) {
// TODO: Handle the error.
//Log.i(TAG, "An error occurred: " + status);
}
});
Button locationSetButton=(Button)findViewById(R.id.locationSetButton);
locationSetButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
LatLng msd=finalLocation;
//getCallingActivity();
Geocoder geocoder=new Geocoder(LocationPickerActivity.this, Locale.getDefault());
List<Address> nearByList=null;
try {
nearByList = geocoder.getFromLocation(finalLocation.latitude,finalLocation.longitude , 1);
}
catch (IOException e){
}
Intent intent=new Intent();
double[] geoCoordinates=new double[2];
geoCoordinates[0]=msd.latitude;
geoCoordinates[1]=msd.longitude;
JSONObject userJSONObject=new JSONObject();
JSONArray userJSONArray=new JSONArray();
try {
Geocoder geocoder2=new Geocoder(LocationPickerActivity.this, Locale.getDefault());
List<Address> nearByList2=null;
HashSet<String> ls2=new HashSet<String>();
try {
nearByList = geocoder.getFromLocation(msd.latitude, msd.longitude, 1);
}
catch (IOException e){
}
Address ad2=nearByList.get(0);
ls2.add(ad2.getPostalCode());
DecimalFormat decimalFormat=new DecimalFormat("#.#####");
double x=Double.parseDouble(decimalFormat.format(msd.latitude));
double y=Double.parseDouble(decimalFormat.format(msd.longitude));
userJSONObject.put("Latitude", x);
userJSONObject.put("Longitude", y);
userJSONObject.put("GoogleAddressLine",ad2.getAddressLine(0));
userJSONObject.put("zipCode",ad2.getPostalCode());
userJSONObject.put("city",ad2.getLocality());
userJSONObject.put("state",ad2.getAdminArea());
userJSONObject.put("country",ad2.getCountryName());
}
catch (JSONException e){
}
userJSONArray.put(userJSONObject);
String data=userJSONObject.toString();
String loc=String.valueOf(geoCoordinates[0])+" * "+String.valueOf(geoCoordinates[1]);
intent.putExtra("locationCoordinates",geoCoordinates);
File locationFile=new File(getApplicationContext().getFilesDir(),"localLocationFile.txt");
try {
FileOutputStream fos = new FileOutputStream(locationFile);
fos.write(data.getBytes());
fos.close();
}
catch (IOException e){
}
finish();
}
});
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
final TextView textView=(TextView)findViewById(R.id.addressTextView);
textView.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
mMap.setPadding(0, textView.getHeight(), 0, 0);
}
}
);
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng latLng) {
finalLocation=latLng;
MarkerOptions markerOptions=new MarkerOptions();
markerOptions.position(latLng);
mMap.clear();
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng,15));
mMap.addMarker(markerOptions);
}
});
mFusedLocationClient= LocationServices.getFusedLocationProviderClient(this);
mLocationRequest=new LocationRequest();
mLocationRequest.setPriority(LocationRequest.PRIORITY_LOW_POWER);
mLocationCallback=new LocationCallback(){
@Override
public void onLocationResult(LocationResult locationResult)
{
if(locationResult==null){
return;
}
for(Location location : locationResult.getLocations())
{
a=location.getLatitude();
b=location.getLongitude();
LatLng ll=new LatLng(a,b);
finalLocation=ll;
addPointToViewPort(ll);
markerLocation=mMap.addMarker(new MarkerOptions().position(ll).draggable(true));
mMap.moveCamera(CameraUpdateFactory.newLatLng(ll));
mMap.animateCamera(CameraUpdateFactory.zoomTo(17));
try{
mMap.setMyLocationEnabled(true);
}
catch(SecurityException e){
}
Geocoder geocoder=new Geocoder(LocationPickerActivity.this, Locale.getDefault());
List<Address> nearByList=null;
HashSet<String> ls=new HashSet<String>();
try {
nearByList = geocoder.getFromLocation(a, b, 1);
}
catch (IOException e){
}
Address ad=nearByList.get(0);
ls.add(ad.getPostalCode());
}
}
};
try {
mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, null);
}
catch (SecurityException e){
}
}
}
您是否正在使用特殊的庫來顯示osm映射? 或嘗試在網絡視圖上加載地圖?
如果您不使用任何特殊的庫,則可以使用OSMSHARP並使用以下代碼可以將OSM映射添加到Xamarin.Android或Xamarin.ios項目:
Native.Initialize();
var _mapView = new MapView(View.Frame);
_mapView.MapCenter = new GeoCoordinate(52.207767, 8.803513);
_mapView.MapZoom = 12;
_mapView.Map.AddLayerTile("http://a.tile.openstreetmap.de/tiles/osmde/{0}/{1}/{2}.png");
View.AddSubview(_mapView);
此代碼會將mapView添加到您的根視圖。
如果您還有其他問題,可以與包裹所有者聯系。
另一個解決方案是使用Web視圖顯示osm映射。 首先,您應該在活動中添加一個Web視圖,然后應該在解決方案中添加Javascript接口類,使用該類可以將交互保存在設備上。
首先,我們定義一個我們要用作或javascript接口的類:
class MyJSInterface : Java.Lang.Object, Java.Lang.IRunnable
{
Context context;
public MyJSInterface (Context context)
{
this.context = context;
}
public void AddNewPlace()
{
Toast.MakeText(context, "Hello from C#", ToastLength.Short).Show();
}
}
下一步是在活動中定義Web視圖,並將此類綁定為Web視圖的接口
WebView webView = new WebView(this);
SetContentView(webView);
webView.Settings.JavaScriptEnabled = true;
webView.AddJavascriptInterface(new MyJSInterface(this), "deviceInterface");
webView.LoadData("PATH TO OSM PAGE THAT HOSTED IN SERVER", "text/html", null);
這是一個簡單的html頁面示例,我們將其加載到webView中以及如何從頁面調用c#方法:
<html>
<body>
<p>calling C# methods from JavaScript</p>
<button type=""button"" onClick=""deviceInterface.AddNewPlace()"">Add Custom Marker or anything else</button>
</body>
</html>
現在我們可以從javascript調用此接口類方法:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.