Google Maps Api Draw Line Between Two Points Python
Simple application that draws a poly line between two points on google map.
(1) Open android studio.
(2) Click File→New→New Project.
(3) Set default values on Create Android Project and Target Android Devices (Phone and Tablet).
(4) On Add an Activity to Mobile, select "Google Maps Activity" .
(5) Set default values on Configure Activity and click Finish.
(6) Follow the instructions shown in google_maps_api.xml to get the Google Maps API key and apply it on project.
(7) Edit activity_maps.xml (layout xml file) as shown below.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:map="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="100dp" android:orientation="vertical"> <EditText android:id="@+id/startPoint" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="START POINT" /> <EditText android:id="@+id/endPoint" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="END POINT" /> </LinearLayout> <Button android:id="@+id/drawPolyLine" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Draw a ployline!" /> <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="wrap_content" android:layout_centerHorizontal="true" android:layout_weight="2" android:clickable="true" android:enabled="true" tools:context="com.example.icarus.myapplication.MapsActivity" />
(8) Edit MapsActivity.java (java class where getting location, drawing markers & drawing poly lines are done) as shown below.
import android.graphics.Color; import android.location.Address; import android.location.Geocoder; import android.support.v4.app.FragmentActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; 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.MarkerOptions; import com.google.android.gms.maps.model.PolylineOptions; import java.io.IOException; import java.util.List; public class MapsActivity extends FragmentActivity implements OnMapReadyCallback { private EditText et; //start point private EditText etB; //end point private Button button; //button to draw polyline private GoogleMap mMap; //map LatLng startPoint; //start point's latitude & longitude LatLng endPoint; //end point's latitude & longitude final Geocoder geocoder = new Geocoder(this); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_maps); // 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); } @Override public void onMapReady(GoogleMap googleMap) { mMap = googleMap; et = (EditText) findViewById(R.id.startPoint); etB = (EditText) findViewById(R.id.endPoint); button = (Button) findViewById(R.id.drawPolyLine); button.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View view) { String str = et.getText().toString(); //start point String strB = etB.getText().toString();//end point List<Address> list = null; //start point's location address List<Address> listB = null; //end point's location address try { list = geocoder.getFromLocationName(str, 10); listB = geocoder.getFromLocationName(strB, 10); } catch (IOException e) { e.printStackTrace(); } String total_str = list.get(0).toString(); //list -> string //parsing start point's latitude int position = total_str.indexOf("latitude"); String str1 = total_str.substring(position + 9); int position_comma = str1.indexOf(","); String latitude = str1.substring(0, position_comma); //parsing start point's longitude int position2 = total_str.indexOf("longitude"); String str2 = total_str.substring(position2 + 10); int position_comma2 = str2.indexOf(","); String longitude = str2.substring(0, position_comma2); String total_strB = listB.get(0).toString();//list -> string //parsing end point's latitude int positionB = total_strB.indexOf("latitude"); String strB1 = total_strB.substring(positionB + 9); int position_commaB = strB1.indexOf(","); String latitudeB = strB1.substring(0, position_commaB); //parsing end point's longitude int positionB2 = total_strB.indexOf("longitude"); String strB2 = total_strB.substring(positionB2 + 10); int position_commaB2 = strB2.indexOf(","); String longitudeB = strB2.substring(0, position_commaB2); //parsing respective latitude & longitude to LatLng type startPoint = new LatLng(Double.parseDouble(latitude), Double.parseDouble(longitude)); endPoint = new LatLng(Double.parseDouble(latitudeB), Double.parseDouble(longitudeB)); //adding markers on respective location mMap.addMarker(new MarkerOptions().position(startPoint).title("Start Point: " + str)); mMap.addMarker(new MarkerOptions().position(endPoint).title("End Point: " + strB)); //move the camera mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(startPoint, 12)); //adding a polyline mMap.addPolyline(new PolylineOptions().add(startPoint, endPoint).width(7).color(Color.MAGENTA)); } }); //adding a marker in Sydney and move the camera (initial marker) LatLng sydney = new LatLng(-34, 151); mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney")); mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney)); } } (8) Run the application. The result is as shown below.
Google Maps Api Draw Line Between Two Points Python
Source: https://phitchuria.wordpress.com/2018/01/03/android-drawing-a-poly-line-between-two-points-using-google-maps-api/
0 Response to "Google Maps Api Draw Line Between Two Points Python"
Postar um comentário